Function Returning Value w/o return stmt


 
Thread Tools Search this Thread
Top Forums Programming Function Returning Value w/o return stmt
# 1  
Old 09-28-2009
Function Returning Value w/o return stmt

I am working on a C/Unix application from last 2 years which communicates with other systems using proprietary format of my client. We have a function written in C which returns integer, which is response from other system to the request message initiated by my system. This return value is then assigned to a variable which determines if the transmission was successful or failed. On failure my application is suppose to show some error message to user.
This system has been working proper from long time. We recently got one production issue where application doesn't display error message to user even when communication returns non-zero (Failure!) return code. While analyzing we found that the return statement itself was missing from long time. Now we wonder how the function was returning the correct value working earlier in production w/o return statement.
When we tried to find if there is any change is environment and the only change we could find all the servers who reported issue was recently upgraded to RED HAT Linux 4.0. Now I really don't know how to explain this to client since though this is a fact but I need to have some technical explanation behind this.

Following is the dummy code snippet to explain scenario,

void main ()
{
int responce_code=0;
responce_code=ParseResponse();
if (responce_code != 0)
{
Show_Error();
}
}

int ParseResponse()
{
// We have some parser code to extract the response
log_message("response from other system, %s",respose)
atoi(respose);
}

Now in above statement atoi(respose); should actually be return atoi(respose); in order to get the return code to main(). But still it used to work fine and all of sudden stared failing after upgrade to Red Hat Linux 4 .
It would be great help if some one can explain how this can happen in C for the Linux OS version below RED HAT 4

Thanks in Advance,
Deepak
# 2  
Old 09-28-2009
First off, compile with gcc -Wall then it won't happen again if you pay attention to warnings. A good compile is zero warnings and zero errors, nothing else is acceptable.

When a function returns an int and nothing is returned by the function - then - whatever garbage data was living on the stack is what gets returned. ie., any value is possible. If your code looks like
Code:
int retval=0;
retval=myfunc();
if(retval)
   // good return
else
  // zero == error return

Then any garbage on the stack will result in a "good" return. This is called programming by coincidence, in polite circles, when it actually works.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Function - Make your function return an exit status

Hi All, Good Day, seeking for your assistance on how to not perform my 2nd, 3rd,4th etc.. function if my 1st function is in else condition. #Body function1() { if then echo "exist" else echo "not exist" } #if not exist in function1 my all other function will not proceed.... (4 Replies)
Discussion started by: meister29
4 Replies

2. Shell Programming and Scripting

Return: can only `return' from a function or sourced script

Not sure where the problem is. I can run the script without any issue using the following command. . /opt/app/scripts/cdc_migration.sh But it fails with the below error when I try it this way /opt/app/scripts/cdc_migration.sh /opt/app/scripts/cdc_migration.sh: line 65: return: can only... (1 Reply)
Discussion started by: svajhala
1 Replies

3. Shell Programming and Scripting

Returning and capturing multiple return values from a function

Hi I am pretty confused in returning and capturing multiple values i have defined a function which should return values "total, difference" i have used as #!/usr/bin/ksh calc() { total=$1+$2 echo "$total" diff=$2-$1 echo "$diff" } I have invoked this function as calc 5 8 Now i... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

4. Programming

Returning local string value from a function in C

Hi, If I have a code like this, what are the potential problems do you see? const char* const retString() { return "hello"; /* string literal */ } My questions are: a) Since the string literal which is already a constant read only data (cannot be... (4 Replies)
Discussion started by: royalibrahim
4 Replies

5. Programming

Function Returning Pointer

Hi guys. how a functions such fdopen, ... can return pointer? are these functions use static memory(variables)? (6 Replies)
Discussion started by: majid.merkava
6 Replies

6. Shell Programming and Scripting

sqlplus returning value - remove carriage return '\r' - Please help

Guys - Simple code, i am trying to get a number back from sqlplus call to a query. After that, i need to use that number in a loop. --------------------------------- #!/bin/ksh VALUE=`sqlplus -silent sh/password@sh <<END set pagesize 0 feedback off verify off heading off echo off select... (10 Replies)
Discussion started by: sunshine1974
10 Replies

7. Shell Programming and Scripting

Return a value from called function to the calling function

I have two scripts. script1.sh looks -------------------------------- #!/bin/bash display() { echo "Welcome to Unix" } display ----------------------------- Script2.sh #!/bin/bash sh script1.sh //simply calling script1.sh ------------------------------ (1 Reply)
Discussion started by: mvictorvijayan
1 Replies

8. Shell Programming and Scripting

Returning the name of function used

Hi All In my script, I can call on several functions. I have a logging function that is called by any of these functions. What I would like is some way of identifying which function I am using and pass this to the log function as some parameter. Is there some built in command or way of... (3 Replies)
Discussion started by: kingpin2502
3 Replies

9. Shell Programming and Scripting

returning from a function

Hi all, I am very new to BASH shell programming. I need to return an integer from a function to the caller function. I did this: but it keeps giving me wrong return: Can someone help me out here, please? Thanks (2 Replies)
Discussion started by: alirezan
2 Replies

10. Programming

string returning function

I have two string returning function in ESQL/C char *segment_name(lbuffer) char *lbuffer; {..... and char *get_bpdvalue(f_name) char *f_name; {...... both declared above main() char *get_bpdvalue(); char *segment_name(); my problem is segment_name works on sprintf and strcpy... (5 Replies)
Discussion started by: jisc
5 Replies
Login or Register to Ask a Question