return 语句 (C)return Statement (C)

return 语句终止函数的执行并返回对调用函数的控制。The return statement terminates the execution of a function and returns control to the calling function. 紧接在调用之后在调用函数中恢复执行。Execution resumes in the calling function at the point immediately following the call. return 语句还可以将值返回给调用函数。A return statement can also return a value to the calling function. 有关详细信息,请参阅返回类型See Return Type for more information.


return expression opt ;return expression opt ;

如果表达式存在的话,expression 的值将返回到调用函数。The value of expression, if present, is returned to the calling function. 如果 expression 省略,该函数返回值未定义。If expression is omitted, the return value of the function is undefined. 先计算表达式(如果存在),然后转换为函数返回的类型。The expression, if present, is evaluated and then converted to the type returned by the function. 如果使用返回类型 void 声明函数,包含表达式的 return 语句生成警告,并且该表达式不进行计算。If the function was declared with return type void, a return statement containing an expression generates a warning and the expression is not evaluated.

如果在函数定义中 return 语句未出现,在执行被调用函数的最后一个语句后,控件自动返回到调用函数。If no return statement appears in a function definition, control automatically returns to the calling function after the last statement of the called function is executed. 在这种情况下,当调用该函数时,返回值将未定义。In this case, the return value of the called function is undefined. 如果不需要返回值,声明该函数具有 void 返回类型;否则,默认返回类型为 intIf a return value is not required, declare the function to have void return type; otherwise, the default return type is int.

许多程序员使用括号括住 return 语句的表达式参数。Many programmers use parentheses to enclose the expression argument of the return statement. 但是,C 不需要括号。However, C does not require the parentheses.

此示例演示了 return 语句:This example demonstrates the return statement:

#include <limits.h>  
#include <stdio.h>  

void draw( int i, long long ll );  
long long sq( int s );  

int main()  
    long long y;  
    int x = INT_MAX;  

    y = sq( x );  
    draw( x, y );  
    return x;  

long long sq( int s )  
    // Note that parentheses around the return expression   
    // are allowed but not required here.  
    return( s * (long long)s );  

void draw( int i, long long ll )  
    printf( "i = %d, ll = %lld\n", i, ll );  

在此示例中,main 函数调用两个函数 sqdrawIn this example, the main function calls two functions: sq and draw. sq 函数返回 x * x 的值为 main,其中返回值分配给 yThe sq function returns the value of x * x to main, where the return value is assigned to y. sq 中的返回表达式两边的括号作为表达式的一部分进行计算,返回语句不需要括号。The parentheses around the return expression in sq are evaluated as part of the expression, and are not required by the return statement. 由于先计算返回表达式,然后将其转换为返回类型,因此 sq 强制表达式类型为使用强制转换的返回类型,以防止可能导致意外结果的整数溢出。Since the return expression is evaluated before it is converted to the return type, sq forces the expression type to be the return type with a cast to prevent a possible integer overflow, which could lead to unexpected results. draw 函数声明为 void 函数。The draw function is declared as a void function. 它打印参数的值,然后空返回语句结束该函数且不返回值。It prints the values of its parameters and then the empty return statement ends the function and does not return a value. 尝试赋值 draw 的返回值会导致发布诊断信息。An attempt to assign the return value of draw would cause a diagnostic message to be issued. 随后,main 函数将 x 的值返回给操作系统。The main function then returns the value of x to the operating system.

该示例的输出与以下内容类似:The output of the example looks like this:

i = 2147483647, ll = 4611686014132420609  

请参阅See Also