Parameter declarations

The function declarator includes the list of parameters that can be passed to the function when it is called by another function, or by itself.

C++ only In C++, the parameter list of a function is referred to as its signature. The name and signature of a function uniquely identify it. As the word itself suggests, the function signature is used by the compiler to distinguish among the different instances of overloaded functions.

Read syntax diagramSkip visual syntax diagram
Function parameter declaration syntax

      .-,-------------.                  
      V               |                  
>>-(----+-----------+-+--+--------+--)-------------------------><
        '-parameter-'    '-,--...-'      

Read syntax diagramSkip visual syntax diagram
parameter

>>-+----------+--type_specifier--+------------+----------------><
   '-register-'                  '-declarator-'   

C++ only An empty argument list in a function declaration or definition indicates a function that takes no arguments. To explicitly indicate that a function does not take any arguments, you can declare the function in two ways: with an empty parameter list, or with the keyword void:
      int f(void);
      int f();
Begin C only An empty argument list in a function definition indicates that a function that takes no arguments. An empty argument list in a function declaration indicates that a function may take any number or type of arguments. Thus,
int f()
{
...
}
indicates that function f takes no arguments. However,
int f();
simply indicates that the number and type of parameters is not known. To explicitly indicate that a function does not take any arguments, you should define the function with the keyword void. End C only
An ellipsis at the end of the parameter specifications is used to specify that a function has a variable number of parameters. The number of parameters is equal to, or greater than, the number of parameter specifications.
      int f(int, ...);

C++ only The comma before the ellipsis is optional. In addition, a parameter declaration is not required before the ellipsis.

C At least one parameter declaration, as well as a comma before the ellipsis, are both required in C.

Parameter types

In a function declaration, or prototype, the type of each parameter must be specified. C++ only In the function definition, the type of each parameter must also be specified. C only In the function definition, if the type of a parameter is not specified, it is assumed to be int.

A variable of a user-defined type may be declared in a parameter declaration, as in the following example, in which x is declared for the first time:
struct X { int i; };
void print(struct X x);
C only The user-defined type can also be defined within the parameter declaration. C++ only The user-defined type can not be defined within the parameter declaration.
void print(struct X { int i; } x);   // legal in C
void print(struct X { int i; } x);   // error in C++

Parameter names

In a function definition, each parameter must have an identifier. In a function declaration, or prototype, specifying an identifier is optional. Thus, the following example is legal in a function declaration:
int func(int,long);
Begin C++ only The following constraints apply to the use of parameter names in function declarations:
  • Two parameters cannot have the same name within a single declaration.
  • If a parameter name is the same as a name outside the function, the name outside the function is hidden and cannot be used in the parameter declaration. In the following example, the third parameter name intersects is meant to have enumeration type subway_line, but this name is hidden by the name of the first parameter. The declaration of the function subway() causes a compile-time error because subway_line is not a valid type name because the first parameter name subway_line hides the namespace scope enum type and cannot be used again in the second parameter.
    enum subway_line {yonge,
    university, spadina, bloor};
    int subway(char * subway_line, int stations,
                      subway_line intersects);
End C++ only

Static array indices in function parameter declarations (C only)

Except in certain contexts, an unsubscripted array name (for example, region instead of region[4]) represents a pointer whose value is the address of the first element of the array, provided that the array has previously been declared. An array type in the parameter list of a function is also converted to the corresponding pointer type. Information about the size of the argument array is lost when the array is accessed from within the function body.

To preserve this information, which is useful for optimization, you may declare the index of the argument array using the static keyword. The constant expression specifies the minimum pointer size that can be used as an assumption for optimizations. This particular usage of the static keyword is highly prescribed. The keyword may only appear in the outermost array type derivation and only in function parameter declarations. If the caller of the function does not abide by these restrictions, the behavior is undefined.

The following examples show how the feature can be used.
void foo(int arr [static 10]);       /* arr points to the first of at least
                                           10 ints                           */
void foo(int arr [const 10]);        /* arr is a const pointer               */
void foo(int arr [static const i]);  /* arr points to at least i ints;
                                           i is computed at run time.        */
void foo(int arr [const static i]);  /* alternate syntax to previous example */
void foo(int arr [const]);           /* const pointer to int                 */