Linux Shell script programming -- difference between return and exit

Source: Internet
Author: User
Tags exit in

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.