6.9.1 Function definitions

Previous Table of Contents "New C Standard" commentary

1821

function-definition:
          declaration-specifiers declarator declaration-listopt compound-statement

declaration-list: declaration declaration-list declaration

1822 The identifier declared in a function definition (which is the name of the function) shall have a function type, as specified by the declarator portion of the function definition.138)

1823 The return type of a function shall be void or an object type other than array type.

1824 The storage-class specifier, if any, in the declaration specifiers shall be either extern or static.

1825 If the declarator includes a parameter type list, the declaration of each parameter shall include an identifier, except for the special case of a parameter list consisting of a single parameter of type void, in which case there shall not be an identifier.

1826 No declaration list shall follow.

1827 If the declarator includes an identifier list, each declaration in the declaration list shall have at least one declarator, those declarators shall declare only identifiers from the identifier list, and every identifier in the identifier list shall be declared.

1828 An identifier declared as a typedef name shall not be redeclared as a parameter.

1829 The declarations in the declaration list shall contain no storage-class specifier other than register and no initializations.

1830 138) The intent is that the type category in a function definition cannot be inherited from a typedef:


        typedef int F(void);             // type F is "function with no parameters
                                         //                returning int"
        F f, g;                          // f and g both have type compatible with F
        F f { /* ... */ }                // WRONG: syntax/constraint error
        F g() { /* ... */ }              // WRONG: declares that g returns a function
        int f(void) { /* ... */ }        // RIGHT: f has type compatible with F
        int g() { /* ... */ }            // RIGHT: g has type compatible with F
        F *e(void) { /* ... */ }         // e returns a pointer to a function
        F *((e))(void) { /* ... */ }     // same: parentheses irrelevant
        int (*fp)(void);                 // fp points to a function that has type F
        F *Fp;                           // Fp points to a function that has type F

1831 The declarator in a function definition specifies the name of the function being defined and the identifiers of its parameters.

1832 If the declarator includes a parameter type list, the list also specifies the types of all the parameters;

1833 such a declarator also serves as a function prototype for later calls to the same function in the same translation unit.

1834 If the declarator includes an identifier list,139) the types of the parameters shall be declared in a following declaration list.

1835 In either case, the type of each parameter is adjusted as described in 6.7.5.3 for a parameter type list;

1836 the resulting type shall be an object type.

1837 If a function that accepts a variable number of arguments is defined without a parameter type list that ends with the ellipsis notation, the behavior is undefined.

1838 Each parameter has automatic storage duration.

1839 Its identifier is an lvalue, which is in effect declared at the head of the compound statement that constitutes the function body (and therefore cannot be redeclared in the function body except in an enclosed block).

1840 The layout of the storage for parameters is unspecified.

1841 On entry to the function, the size expressions of each variably modified parameter are evaluated and the value of each argument expression is converted to the type of the corresponding parameter as if by assignment.

1842 (Array expressions and function designators as arguments were converted to pointers before the call.)

1843 After all parameters have been assigned, the compound statement that constitutes the body of the function definition is executed.

1844 If the } that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined.

1845 EXAMPLE 1 In the following:


        extern int max(int a, int b)
        {
                return a > b ? a : b;
        }

extern is the storage-class specifier and int is the type specifier; max(int a, int b) is the function declarator; and


        { return a > b ? a : b; }

is the function body. The following similar definition uses the identifier-list form for the parameter declarations:


        extern int max(a, b)
        int a, b;
        {
                return a > b ? a : b;
        }

Here int a, b; is the declaration list for the parameters. The difference between these two definitions is that the first form acts as a prototype declaration that forces conversion of the arguments of subsequent calls to the function, whereas the second form does not.

1846 139) See “future language directions” (6.11.7).

1847 EXAMPLE 2 To pass one function to another, one might say


                int f(void);
                /* ... */
                g(f);

Then the definition of g might read


        void g(int (*funcp)(void))
        {
                /* ... */
                (*funcp)(); /* or funcp()  ...  */
        }

or, equivalently,


        void g(int func(void))
        {
                /* ... */
                func(); /* or (*func)()  ...  */
        }

Next

Created at: 2008-01-30 02:39:44 The text from WG14/N1256 is copyright © ISO