Return Statement VS Exit() in Main()

return statement vs exit() in main()

Actually, there is a difference, but it's subtle. It has more implications for C++, but the differences are important.

When I call return in main(), destructors will be called for my locally scoped objects. If I call exit(), no destructor will be called for my locally scoped objects! Re-read that. exit() does not return. That means that once I call it, there are "no backsies." Any objects that you've created in that function will not be destroyed. Often this has no implications, but sometimes it does, like closing files (surely you want all your data flushed to disk?).

Note that static objects will be cleaned up even if you call exit(). Finally note, that if you use abort(), no objects will be destroyed. That is, no global objects, no static objects and no local objects will have their destructors called.

Proceed with caution when favoring exit over return.

http://groups.google.com/group/gnu.gcc.help/msg/8348c50030cfd15a

When to use exit() over return?

These two are very different in nature.

  • exit() is used when you want to terminate program immediately. If a call to exit() is encountered from any part of the application, the application finishes execution.
  • return is used to return the program execution control to the caller function. In case of main() only, return finishes the execution.

EDIT:

To clarify about the case when used in main(), quoting directly from the C11 standard, chapter §5.1.2.2.3, Program termination,

If the return type of the main() function is a type compatible with int, a return from the initial call to the main() function is equivalent to calling the exit() function with the value returned by the main() function as its argument;11) reaching the } that terminates the
main() function returns a value of 0. If the return type is not compatible with int, the termination status returned to the host environment is unspecified.

So, basically, either

  • return 0;
  • exit(0);

will behave as same in the context of main().

Difference between return 0 and exit (0)

return exits from the function while exit exits from the program.

In main function executing return 0; statement or calling exit(0) function will call the registered atexit handlers and will cause program termination.

does return 0 exit a program or exit a loop?

It will return from the main method, which in this case, will exit the whole program.

Java - Could a return statement be used to exit a program?

Executing return from the main() method will exit the whole program.


Caveat: If you start (non-daemon) threads, the JVM will wait for those threads to finish before shutting down.

Difference between return and exit in Bash functions

From man bash on return [n];

Causes a function to stop executing and return the value specified by n to its caller. If n is omitted, the return status is that of the last command executed in the function body.

... on exit [n]:

Cause the shell to exit with a status of n. If n is omitted, the exit status is that of the last command executed. A trap on EXIT is executed before the shell terminates.

EDIT:

As per your edit of the question, regarding exit codes, return has nothing to do with exit codes. Exit codes are intended for applications/scripts, not functions. So in this regard, the only keyword that sets the exit code of the script (the one that can be caught by the calling program using the $? shell variable) is exit.

EDIT 2:

My last statement referring exit is causing some comments. It was made to differentiate return and exit for the understanding of the OP, and in fact, at any given point of a program/shell script, exit is the only way of ending the script with an exit code to the calling process.

Every command executed in the shell produces a local "exit code": it sets the $? variable to that code, and can be used with if, && and other operators to conditionally execute other commands.

These exit codes (and the value of the $? variable) are reset by each command execution.

Incidentally, the exit code of the last command executed by the script is used as the exit code of the script itself as seen by the calling process.

Finally, functions, when called, act as shell commands with respect to exit codes. The exit code of the function (within the function) is set by using return. So when in a function return 0 is run, the function execution terminates, giving an exit code of 0.



Related Topics



Leave a reply



Submit