Differences between return and exit in Linux Programming
Exit is used to end the execution of a program, while return is only used to return from a function.
Return
Return indicates that the function is returned from the called function to the main function for execution. A return value can be included in the return value, which is specified by the parameter following return. Of course, if the main function is used, naturally, the current process is terminated. If not, the previous invocation is returned.
Return is usually necessary, because the computation results are usually obtained through the return value during function calls.
If the function execution does not need to return the calculation result, a status code is often returned to indicate whether the function execution is successful (-1 and 0 are the most common status codes ), the main function can determine the execution status of the called function through the return value.
If you do not need the function to return any value, you need to use void to declare its type.
Supplement: If you have a return type definition before the function name, such as Int or double, you must have a return value. If it is void, you can leave the return value blank, however, even if a value is written
For example:
1. Non-void
Int F1 ()
{
Int I = 1;
Return 1;
// Return (I); // You can also
}
2. Void
Void F2 ()
{
Int I = 1;
// Return; // This can be done either.
}
Sometimes, even if the called function is void, the return in the called function is not meaningless.
Example:
# Include "stdio. H"
Void function ()
{Printf ("111111 ");
Return;
Printf ("222222 ");
}
Main ()
{Function ();
}
The running result is: only a string of numbers 111111 and no 222222 are output on the screen. However, if the return statement in the function is removed, a series of numbers 222222 can be output simultaneously.
Exit ()
Exit (0) indicates that the program is exited normally. If another value is added: 1, 2,..., it indicates that the program exits due to different error causes.
So, how does 1, 2, 3 correspond to different reasons? -- What do you want it to mean? What does it mean?
But it generally has a common meaning: for example, 0 usually indicates normal return and exit.
Therefore, exit (0) in the main function is equivalent to return 0.