Usage of return in C language
The return value of a function refers to the value obtained by executing the code in the function body after the function is called. As a result, this result is returned through the return statement. The general form of the
return statement is:
return 表达式;
or:
return (表达式);
It is correct with or without ( ). For the sake of simplicity, ( ) is generally not written. For example:
return max; return a+b; return (100+200);
Recommended learning: c language video tutorial
1. There can be multiple return statements, which can appear anywhere in the function body, but each call A function can only have one return statement executed, so there is only one return value (a few programming languages support multiple return values, such as Go language). For example:
//返回两个整数中较大的一个 int max(int a, int b){ if(a > b){ return a; }else{ return b; } }
If a>b is true, return a will be executed, and return b will not be executed; if it is not true, return b will be executed, and return a will not be executed.
2. Once the function encounters the return statement, it will return immediately, and all subsequent statements will not be executed. From this perspective, the return statement also has the function of forcibly ending function execution. For example:
//返回两个整数中较大的一个 int max(int a, int b){ return (a>b) ? a : b; printf("Function is performed\n"); }
The 4th line of code is redundant and will never have a chance to be executed.
For more c language tutorials, please pay attention to PHP Chinese website!
The above is the detailed content of How to use return in c language. For more information, please follow other related articles on the PHP Chinese website!