What does a return 0 or 1 means and where and to whom does it return it to?
What does a return 0 or 1 means and where and to whom does it return it to?
'return 0' return's 0 and 'return 1' return's 1 to the calling function/process. The choice of 0 or 1 (or whatever) is purely with the user.
Ex:
In this example, a value of 0 is returned to the the calling function - fn1(), by the called function fn2().Code:int fn2(void) { return(0); } void fn1(void) { printf(" %d\n", fn1()); }
Similarly;
A value of 1 is returned the the parent of this process (usually the shell).Code:int main(void) { return(1); }
Usually a function or program returns a 0 on success and an error code on failure. The main deviation from this is when a function is supposed to return a pointer. When the function is not successful, it should return the NULL pointer (0).
Small typo..., it must be:Originally posted by shaik786
Code:void fn1(void) { printf(" %d\n", fn1()); }
Code:printf(" %d\n", f2());
So if I return a value any value to a calling function in the main program what happen to that value...can it be used in the main program for other purposes
>So if I return a value any value to a calling function in the main program what happen to that value...
It kind of depends on the type of the value and how it was created or used in the function. But simply, you can use a return value in the calling function for whatever you need. Usually the return value is a flag indicating whether or not the function terminated successfully:
-PreludeCode:int func ( void ) { /* ** Do stuff. */ if ( stuff == GOOD ) return 0; else return 1; } void calling_function ( void ) { int success_flag; success_flag = func(); if ( success_flag == 0 ) { /* ** All is well */ } else { /* ** Handle the error. */ } }
My best code is written with the delete key.
And by the way, I wonder what the mean by return -1.
It means whatever the original programmer intended it to mean. Yes, there are plenty of standard functions that return -1 on error, but that isn't always guaranteed to be the case. You need to read up on each function you use to understand what it will return under all conditions.Originally posted by June
And by the way, I wonder what the mean by return -1.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]