参数Parameters

自变量是通过函数调用传递到函数的值的名称。Arguments are names of values passed to a function by a function call. 形参是函数期望接收的值。Parameters are the values the function expects to receive. 在函数原型中,函数名称后的括号包含函数的参数及其类型的完整列表。In a function prototype, the parentheses following the function name contain a complete list of the function's parameters and their types. 参数声明指定参数中存储的值的类型、大小和标识符。Parameter declarations specify the types, sizes, and identifiers of values stored in the parameters.

语法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 */

declarator :declarator :
pointer optdirect-declaratorpointer optdirect-declarator

direct-declarator:/* 函数声明符 */direct-declarator:/* A function declarator */
direct-declarator ( parameter-type-list ) /* 新样式声明符 */direct-declarator ( parameter-type-list ) /* New-style declarator */

parameter-type-list:/* 参数列表 */parameter-type-list: /* A parameter list */
parameter-listparameter-list

parameter-list ,...parameter-list ,...

parameter-list:parameter-list:
parameter-declarationparameter-declaration

parameter-list , parameter-declarationparameter-list , parameter-declaration

parameter-declaration:parameter-declaration:
declaration-specifiers declaratordeclaration-specifiers declarator

declaration-specifiers abstract-declarator optdeclaration-specifiers abstract-declarator opt

parameter-type-list 是一系列以逗号分隔的参数声明。The parameter-type-list is a sequence of parameter declarations separated by commas. 参数列表中的每个参数的格式如下所示:The form of each parameter in a parameter list looks like this:

[register]  type-specifier [declarator]   

用 auto 特性声明的函数参数会产生错误。Function parameters declared with the auto attribute generate errors. 参数的标识符在函数体中使用以引用传递给函数的值。The identifiers of the parameters are used in the function body to refer to the values passed to the function. 您可以在原型中命名参数,但名称会超出声明的末尾的范围。You can name the parameters in a prototype, but the names go out of scope at the end of the declaration. 因此,在函数定义中可以用相同或不同的方式分配参数名称。Therefore parameter names can be assigned the same way or differently in the function definition. 这些标识符不能在函数体的最外面的块中重新定义,但它们可在内部的嵌套块中定义,就像参数列表位于封闭块中一样。These identifiers cannot be redefined in the outermost block of the function body, but they can be redefined in inner, nested blocks as though the parameter list were an enclosing block.

parameter-type-list 中的每个标识符的前面必须是其合适的类型说明符,如以下示例所示:Each identifier in parameter-type-list must be preceded by its appropriate type specifier, as shown in this example:

void new( double x, double y, double z )  
{  
    /* Function body here */  
}  

如果至少有一个参数出现在参数列表中,则该列表可以以一个逗号后跟三个句点 (, ...) 结尾。此构造称为“省略号表示法”,表示函数的可变数量的自变量。If at least one parameter occurs in the parameter list, the list can end with a comma followed by three periods (, ...). This construction, called the "ellipsis notation," indicates a variable number of arguments to the function. (有关详细信息,请参阅使用可变数量的参数进行调用。)但是,对函数进行调用时,自变量的数量必须至少与最后一个逗号前面的参数的数量相同。(See Calls with a Variable Number of Arguments for more information.) However, a call to the function must have at least as many arguments as there are parameters before the last comma.

如果实参不会传递到函数,则形参的列表将替换为关键字 voidIf no arguments are to be passed to the function, the list of parameters is replaced by the keyword void. void 的这种用法不同于将其用作类型说明符。This use of void is distinct from its use as a type specifier.

参数的顺序和类型(包括省略号表示法的任何用法)在所有函数声明(如果有)和函数定义中都必须相同。The order and type of parameters, including any use of the ellipsis notation, must be the same in all the function declarations (if any) and in the function definition. 进行常用算术转换后,自变量的类型与对应参数的类型必须是赋值兼容的。The types of the arguments after usual arithmetic conversions must be assignment-compatible with the types of the corresponding parameters. (有关算术转换的信息,请参阅常用算术转换。)不检查省略号后面的自变量。(See Usual Arithmetic Conversions for information on arithmetic conversions.) Arguments following the ellipsis are not checked. 参数可以具有任何基础、结构、联合、指针或数组类型。A parameter can have any fundamental, structure, union, pointer, or array type.

如果需要,编译器将独立于每个参数和每个自变量执行常用算术转换。The compiler performs the usual arithmetic conversions independently on each parameter and on each argument, if necessary. 转换后,没有参数短于 int 且没有参数具有 float 类型,除非参数类型在原型中显式指定为 float。After conversion, no parameter is shorter than an int, and no parameter has float type unless the parameter type is explicitly specified as float in the prototype. 这意味着,例如,将参数声明为 char 与将其声明为 int 的效果相同。This means, for example, that declaring a parameter as a char has the same effect as declaring it as an int.

另请参阅See Also

C 函数定义C Function Definitions