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
- If, return implementation
- 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