C 函数定义C Function Definitions

函数定义指定函数的名称、函数期望接收的参数的类型和数量以及函数的返回类型。A function definition specifies the name of the function, the types and number of parameters it expects to receive, and its return type. 函数定义还包括带有局部变量的声明的函数体和确定函数行为的语句。A function definition also includes a function body with the declarations of its local variables, and the statements that determine what the function does.

语法Syntax

translation-unit:translation-unit:
external-declarationexternal-declaration

translation-unit external-declarationtranslation-unit external-declaration

external-declaration: /* 只允许在外部(文件)范围内 */external-declaration: /* Allowed only at external (file) scope */
function-definitionfunction-definition

declaration

function-definition: /* 此处的声明符是函数声明符 */function-definition: /* Declarator here is the function declarator */
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 */

原型参数为:Prototype parameters are:

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

declaration-list:declaration-list:
declarationdeclaration

declaration-list declarationdeclaration-list declaration

declaratordeclarator:
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 */

direct-declarator ( identifier-list opt) /* 旧样式声明符 */direct-declarator ( identifier-list opt) /* Obsolete-style declarator */

定义中的参数列表使用以下语法:The parameter list in a definition uses this syntax:

parameter-type-list: /* 参数列表 */parameter-type-list: /* The 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

旧式函数定义中的参数列表使用以下语法:The parameter list in an old-style function definition uses this syntax:

identifier-list:/* 在旧式函数定义和声明中使用 */identifier-list: /* Used in obsolete-style function definitions and declarations */
identifieridentifier

identifier-list , identifieridentifier-list , identifier

函数体的语法为:The syntax for the function body is:

compound-statement:/* 函数体 */compound-statement: /* The function body */
{ declaration-list optstatement-list opt}{ declaration-list optstatement-list opt}

仅有的可修改函数声明的存储类说明符是 extern 和 static。The only storage-class specifiers that can modify a function declaration are extern and static. extern 说明符表示可以从其他文件引用函数;即,将函数名导出到链接器。The extern specifier signifies that the function can be referenced from other files; that is, the function name is exported to the linker. static 说明符表示不能从其他文件引用函数;即,链接器不会导出名称。The static specifier signifies that the function cannot be referenced from other files; that is, the name is not exported by the linker. 如果存储类未在函数定义中出现,则假定 externIf no storage class appears in a function definition, extern is assumed. 在任何情况下,从定义点到文件的末尾函数始终可见。In any case, the function is always visible from the definition point to the end of the file.

可选的 declaration-specifiers 和必需的 declarator 共同指定函数的返回类型和名称。The optional declaration-specifiers and mandatory declarator together specify the function's return type and name. declarator 是用来命名函数的标识符与函数名后面的括号的组合。The declarator is a combination of the identifier that names the function and the parentheses following the function name. 可选的 attribute-seq 非终止符是在函数特性中定义的 Microsoft 专用功能。The optional attribute-seq nonterminal is a Microsoft-specific feature defined in Function Attributes.

direct-declarator(在 declarator 语法中)指定要定义的函数的名称及其参数的标识符。The direct-declarator (in the declarator syntax) specifies the name of the function being defined and the identifiers of its parameters. 如果 direct-declarator 包括 parameter-type-list,则该列表将指定所有参数的类型。If the direct-declarator includes a parameter-type-list, the list specifies the types of all the parameters. 此类声明符还用作以后对函数进行调用时的函数原型。Such a declarator also serves as a function prototype for later calls to the function.

函数定义中的 declaration-list 内的 declaration 不能包含除 register 之外的 storage-class-specifier。A declaration in the declaration-list in function definitions cannot contain a storage-class-specifier other than register. 仅当为 int 类型的值指定 register 存储类时,才能省略 declaration-specifiers 语法中的 type-specifier。The type-specifier in the declaration-specifiers syntax can be omitted only if the register storage class is specified for a value of int type.

compound-statement 是包含局部变量声明、对在外部声明的项的引用和语句的函数体。The compound-statement is the function body containing local variable declarations, references to externally declared items, and statements.

函数特性存储类返回类型参数函数体节详细地描述了函数定义的组成部分。The sections Function Attributes, Storage Class, Return Type, Parameters, and Function Body describe the components of the function definition in detail.

另请参阅See Also

函数Functions