Using output (1) to return from a function

linux gcc 4.4.1 C99

I'm just wondering if there are any advantages using the following methods. I noticed that with some code that I read, the exit number increased in value, as shown in this code snippet.

/* This would happen in 1 function */ if(test condition 1) { /* something went wrong */ exit(1); } if(test condition 2) { /* something went wrong with another condition*/ exit(2); } 

or follow these steps and just go back:

 /* This would happen in 1 function */ if(test condition 1) { /* something went wrong */ return; } if(test condition 2) { /* something went wrong with another condition*/ return; } 
+4
source share
7 answers

exit () terminates your entire program and sends back the argument you pass. This allows any programs that run your program to find out why it failed. (1 may indicate a failure to connect to the database, 2 may mean unexpected arguments, etc.).

The return is returned only from the current function in which you are located, and not for the entire program.

+23
source

return will basically return from the function and adjust the stack pointers accordingly to execute the following commands, where exit will exit program.

using exit() in the function indicates a fatal error, and the program cannot recover and continue, and therefore, it must be completed.

+6
source

exit will not return from the function. He will quit the whole program.

+4
source

I don’t think you want to quit the whole program, right?

So just returning from the function will be ok.

 /* This would happen in 1 function */ if(test condition 1) { /* something went wrong */ return; /*return type must be void in this case */ } if(test condition 2) { /* something went wrong with another condition*/ return; /*return type must be void in this case */ } 

You can also explicitly specify the type of function return and use the return value to judge whether everything is okay or not.

+3
source

You ask us if you should return error codes from your functions?

Well, it depends on how informative you want to be for your users. If you want to act like software, it usually acts and a modal dialogue appears that says

Something bad happened!

Then there is no need for return codes.

If, however, you want your software to be useful to your users and let them know what happened, then you better provide some kind of diagnostic information (at least error codes). Then you can display a message that says:

I can not open "foo.bar".

Does this file exist? Do you have access to it? Is it part of the network? Maybe I should try again?

+2
source

This is usually the case that the program running your program can make smart decisions. For example, if there is a script wrapper around your foo program, then it can check exit using the $$ variable and change the execution path:

exec foo if $$ eq '0': success echo elif $$ eq '2': exec error-recovery- script

Alternatively, you yourself can run echo $$ to find out what exit code is.

+2
source

If the idea is to return a value based on a test condition, prefer to use return instead of output.

To return 1, instead of exit (1), use return 1.

Do not report reasons, as this link has been discussed in detail).

0
source

All Articles