Different implementation forms of multiple return in a function

Source: Internet
Author: User

Function of return

1. Return a value to the function. After the main function calls this function, the returned value is obtained.
Ii. End the function. For example, if you run it in one place, you can use the return statement to end the function without continuing to run the code.

 

Two implementations

    1. If, return implementation
    2. Do {} while (false); Implementation

If, return implementation

int if_return_func() {    result = 0;    if (condition1) {        return result1;    }    if (condition2) {        return result2;    }    if (condition3) {        return result3;    }    return result;}

 

Do {} while (false); Implementation

int do_while_return_func() {    result = 0;    do{        if (condition1) {            result = result1;            break;        }        if (condition2) {            result = result2;            break;        }        if (condition3) {            result = result3;            break;        }    }while(false);    return result;}

 

Remarks

----------------------------------------------------------------------------------

Introduction to the reutrn statement in C/C ++:
Return a value to the function by using the return statement in the function. Terminate the function call and return the main function.
Format:
Return (expression );
Or return expression;
Function:
(1) calculate the value of the expression and return the value of the expression to the function.
(2) return the main function from the called function.
Note:
(1) the return value type should be the same as the function type. Otherwise, the return value is automatically converted to the function type.
(2) A function can have multiple return statements. In this case, it is generally used together with the if statement to determine which return statement to execute and which return statement to take effect.
(3) If there is no return statement in the function and it is returned when it is executed to the end of the function body, an uncertain value is returned to the function.
(4) If you only need to return from the function without bringing back the value, use a return statement without an expression.

Different implementation forms of multiple return in a function

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.