函数概述Overview of Functions

函数必须具有定义且应具有声明,尽管定义可用作声明(如果声明在调用函数前出现)。Functions must have a definition and should have a declaration, although a definition can serve as a declaration if the declaration appears before the function is called. 函数定义包含函数主体(调用函数时执行的代码)。The function definition includes the function body — the code that executes when the function is called.

函数声明为程序中其他位置定义的函数建立名称、返回类型和特性。A function declaration establishes the name, return type, and attributes of a function that is defined elsewhere in the program. 函数声明必须在对函数的调用之前。A function declaration must precede the call to the function. 这就是为什么在调用运行时函数之前将包含运行时函数的声明的头文件包含在您的代码中的原因。This is why the header files containing the declarations for the run-time functions are included in your code before a call to a run-time function. 如果声明包含有关参数的类型和数目的信息,则该声明为原型。If the declaration has information about the types and number of parameters, the declaration is a prototype. 有关详细信息,请参阅函数原型See Function Prototypes for more information.

编译器使用原型来比较随后通过函数的参数对函数进行的调用中的自变量类型,并在需要时,将自变量类型转换为参数类型。The compiler uses the prototype to compare the types of arguments in subsequent calls to the function with the function's parameters and to convert the types of the arguments to the types of the parameters whenever necessary.

函数调用将执行控制从正在调用的函数传递到已调用函数。A function call passes execution control from the calling function to the called function. 通过值将参数(如果有)传递给已调用函数。The arguments, if any, are passed by value to the called function. 已调用函数中的 return 语句的执行将返回控制,并且可能将一个值返回给正在调用的函数。Execution of a return statement in the called function returns control and possibly a value to the calling function.

另请参阅See Also

函数Functions