参数Arguments

函数调用中的自变量具有此形式:The arguments in a function call have this form:


expression  
(  
expression-list <SUB>opt</SUB> )  /* Function call */  

在函数调用中,expression-list 是表达式的列表(用逗号分隔)。In a function call, expression-list is a list of expressions (separated by commas). 后面这些表达式的值是传递给函数的自变量。The values of these latter expressions are the arguments passed to the function. 如果函数不采用参数,则 expression-list 应包含关键字 voidIf the function takes no arguments, expression-list should contain the keyword void.

自变量可以是具有基本、结构、联合或指针类型的任何值。An argument can be any value with fundamental, structure, union, or pointer type. 通过值传递所有参数。All arguments are passed by value. 这意味着,参数的副本将分配给对应的参数。This means a copy of the argument is assigned to the corresponding parameter. 该函数不了解已传递的参数的实际内存位置。The function does not know the actual memory location of the argument passed. 该函数使用此副本,而不影响其最初派生自的变量。The function uses this copy without affecting the variable from which it was originally derived.

虽然您无法将数组或函数作为参数传递,但可以将指针传递给这些项。Although you cannot pass arrays or functions as arguments, you can pass pointers to these items. 利用指针,函数可以通过引用访问值。Pointers provide a way for a function to access a value by reference. 由于指向变量的指针包含该变量的地址,因此该函数可以使用此地址访问该变量的值。Since a pointer to a variable holds the address of the variable, the function can use this address to access the value of the variable. 指针参数允许函数访问数组和函数,即使数组和函数不能作为参数传递。Pointer arguments allow a function to access arrays and functions, even though arrays and functions cannot be passed as arguments.

计算参数的顺序因不同的编译器和不同的优化级别而异。The order in which arguments are evaluated can vary under different compilers and different optimization levels. 但是,在输入函数之前,将完全计算自变量和任何副作用。However, the arguments and any side effects are completely evaluated before the function is entered. 有关副作用的信息,请参阅副作用See Side Effects for information on side effects.

计算函数调用中的 expression-list,并在函数调用中对每个参数执行常用算术转换。The expression-list in a function call is evaluated and the usual arithmetic conversions are performed on each argument in the function call. 如果原型可用,则生成的自变量类型将与原型的对应参数进行比较。If a prototype is available, the resulting argument type is compared to the prototype's corresponding parameter. 如果这些参数不匹配,则执行转换或发布诊断消息。If they do not match, either a conversion is performed, or a diagnostic message is issued. 参数还执行常用算术转换。The parameters also undergo the usual arithmetic conversions.

除非函数的原型或定义显式指定实参的变量数目,否则 expression-list 中的表达式的数目必须与形参的数目匹配。The number of expressions in expression-list must match the number of parameters, unless the function's prototype or definition explicitly specifies a variable number of arguments. 在这种情况下,编译器将检查与参数列表中的类型名称一样多的自变量,并根据需要转换这些自变量,如上所述。In this case, the compiler checks as many arguments as there are type names in the list of parameters and converts them, if necessary, as described above. 有关详细信息,请参阅使用可变数量的参数进行调用See Calls with a Variable Number of Arguments for more information.

如果原型的参数列表只包含关键字 void,则编译器需要在函数调用和定义中使用零参数。If the prototype's parameter list contains only the keyword void, the compiler expects zero arguments in the function call and zero parameters in the definition. 如果编译器找到任何参数,则会发出该诊断消息。A diagnostic message is issued if it finds any arguments.

示例Example

此示例将指针用作参数:This example uses pointers as arguments:

int main()  
{  
    /* Function prototype */  

    void swap( int *num1, int *num2 );  
    int x, y;  
    .  
    .  
    .  
    swap( &x, &y );  /* Function call */  
}  

/* Function definition */  

void swap( int *num1, int *num2 )  
{  
    int t;  

    t = *num1;  
    *num1 = *num2;  
    *num2 = t;  
}  

在此示例中,swap 函数在 main 中声明且具有两个参数,分别用标识符 num1num2 表示,二者都是指向 int 值的指针。In this example, the swap function is declared in main to have two arguments, represented respectively by identifiers num1 and num2, both of which are pointers to int values. 此外,还将原型样式定义中的参数 num1num2 声明为指向 int 类型值的指针。The parameters num1 and num2 in the prototype-style definition are also declared as pointers to int type values.

在函数调用中In the function call

swap( &x, &y )  

x 的地址存储在 num1 中,而 y 的地址存储在 num2 中。the address of x is stored in num1 and the address of y is stored in num2. 现在,这两个名称或“别名”存在于同一个位置。Now two names, or "aliases," exist for the same location. *num1 中的 *num2swap 的引用是对 x 中的 ymain 的有效引用。References to *num1 and *num2 in swap are effectively references to x and y in main. swap 中的赋值实际交换了 xy 的内容。The assignments within swap actually exchange the contents of x and y. 因此,return 语句不是必需的。Therefore, no return statement is necessary.

编译器对 swap 的参数执行类型检查,因为 swap 的原型包含每个参数的参数类型。The compiler performs type checking on the arguments to swap because the prototype of swap includes argument types for each parameter. 原型和定义的括号内的标识符可能相同,也可能不同。The identifiers within the parentheses of the prototype and definition can be the same or different. 重要的一点是,自变量的类型与原型和定义中的这些参数列表相匹配。What is important is that the types of the arguments match those of the parameter lists in both the prototype and the definition.

另请参阅See Also

函数调用Function Calls