返回类型Return Type

函数的返回类型建立由该函数返回的值的大小和类型,并与以下语法中的 type-specifier 相对应:The return type of a function establishes the size and type of the value returned by the function and corresponds to the type-specifier in the syntax below:

语法Syntax

function-definition:function-definition:
declaration-specifiers optattribute-seq optdeclarator declaration-list optcompound-statementdeclaration-specifiers optattribute-seq optdeclarator declaration-list optcompound-statement

/* attribute-seq 为 Microsoft 专用 //* *attribute-seq is Microsoft Specific */

declaration-specifiersdeclaration-specifiers:
storage-class-specifier declaration-specifiers optstorage-class-specifier declaration-specifiers opt

type-specifier declaration-specifiers opttype-specifier declaration-specifiers opt

type-qualifier declaration-specifiers opttype-qualifier declaration-specifiers opt

type-specifiertype-specifier:
voidvoid

charchar

shortshort

intint

longlong

floatfloat

doubledouble

signedsigned

unsignedunsigned

struct-or-union-specifierstruct-or-union-specifier

enum-specifierenum-specifier

typedef-nametypedef-name

type-specifier 可以指定任何基本、结构或联合类型。The type-specifier can specify any fundamental, structure, or union type. 如果不包含 type-specifier,则假定返回类型 intIf you do not include type-specifier, the return type int is assumed.

函数定义中给定的返回类型必须与程序中其他位置的函数声明中的返回类型相匹配。The return type given in the function definition must match the return type in declarations of the function elsewhere in the program. 当执行包含表达式的 return 语句时,函数将返回值。A function returns a value when a return statement containing an expression is executed. 计算该表达式,转换为返回值类型(如果需要)并返回到调用函数的点。The expression is evaluated, converted to the return value type if necessary, and returned to the point at which the function was called. 如果使用返回类型 void 声明函数,则包含表达式的返回语句会生成警告,并且不计算该表达式。If a function is declared with return type void, a return statement containing an expression generates a warning and the expression is not evaluated.

以下示例阐释函数返回值。The following examples illustrate function return values.

typedef struct    
{  
    char name[20];  
    int id;  
    long class;  
} STUDENT;  

/* Return type is STUDENT: */  

STUDENT sortstu( STUDENT a, STUDENT b )  
{  
    return ( (a.id < b.id) ? a : b );  
}  

该示例定义了带 STUDENT 声明的 typedef 类型并定义了函数 sortstu 以具有 STUDENT 返回类型。This example defines the STUDENT type with a typedef declaration and defines the function sortstu to have STUDENT return type. 函数选择并返回其两个结构参数之一。The function selects and returns one of its two structure arguments. 在对函数的后续调用中,编译器会检查以确保参数类型是 STUDENTIn subsequent calls to the function, the compiler checks to make sure the argument types are STUDENT.

备注

通过传递指向结构的指针而不是整个结构来提高效率。Efficiency would be enhanced by passing pointers to the structure, rather than the entire structure.

char *smallstr( char s1[], char s2[] )  
{  
    int i;  

    i = 0;  
    while ( s1[i] != '\0' && s2[i] != '\0' )  
        i++;  
    if ( s1[i] == '\0' )  
        return ( s1 );  
    else  
        return ( s2 );  
}  

此示例定义了一个返回指向字符数组的指针的函数。This example defines a function returning a pointer to an array of characters. 该函数采用两个字符数组(字符串)作为参数,并返回指向两个字符串中较短的一个字符串的指针。The function takes two character arrays (strings) as arguments and returns a pointer to the shorter of the two strings. 指向数组的指针将指向第一个数组元素并具有其类型;因此,该函数的返回类型是指向类型 char 的指针。A pointer to an array points to the first of the array elements and has its type; thus, the return type of the function is a pointer to type char.

在调用函数之前,您无需使用 int 返回类型声明函数,但建议使用原型,以便启用对参数和返回值的正确类型检查。You need not declare functions with int return type before you call them, although prototypes are recommended so that correct type checking for arguments and return values is enabled.

另请参阅See Also

C 函数定义C Function Definitions