cpp/language/function

A function declaration introduces the function name and its type. A function definition associates the function name/type with the function body.

Function declaration
Function declarations may appear in any scope. A function declaration at class scope introduces a class member function (unless the specifier is used), see  and  for details.

The type of the function being declared is composed from the return type (provided by the of the ) and the function

(see for the other forms of the  syntax)

@1@ Regular function declarator syntax. @2@ Trailing return type declaration. The decl-specifier-seq in this case must contain the keyword.

Function declarators can be mixed with other declarators, where decl-specifier-seq allows:

The return type of a function cannot be a function type or an array type (but can be a pointer or reference to those).

As with any declaration, the type of the function declared as  is  (except for parameter type rewriting described below): see.

{{rrev|since=c++14|

Return type deduction
If the decl-specifier-seq of the function declaration contains the keyword, trailing return type may be omitted, and will be deduced by the compiler from the type of the expression used in the statement. If the return type does not use, the deduction follows the rules of :

If the return type is, the return type is as what would be obtained if the expression used in the return statement were wrapped in :

(note: "" is an error, must be used on its own)

If there are multiple return statements, they must all deduce to the same type:

If there is no return statement or if the argument of the return statement is a void expression, the declared return type must be either, in which case the deduced return type is , or (possibly cv-qualified) , in which case the deduced return type is then (identically cv-qualified) :

Once a return statement has been seen in a function, the return type deduced from that statement can be used in the rest of the function, including in other return statements:

If the return statement uses a braced-init-list, deduction is not allowed:

cannot use return type deduction:

other than can use return type deduction. The deduction takes place at instantiation even if the expression in the return statement is not. This instantiation is not in an immediate context for the purposes of.

Redeclarations or specializations of functions or function templates that use return type deduction must use the same return type placeholders:

Similarly, redeclarations or specializations of functions or function templates that do not use return type deduction must not use a placeholder:

do not themselves instantiate function templates that use return type deduction:

}}

Parameter list
Parameter list determines the arguments that can be specified when the function is called. It is a comma-separated list of parameter declarations, each of which has the following syntax:

@1@ Declares a named (formal) parameter. For the meanings of and, see.

@2@ Declares a named (formal) parameter with a.

@3@ Declares an unnamed parameter.

@4@ Declares an unnamed parameter with a.

@5@ Indicates that the function takes no parameters, it is the exact synonym for an empty parameter list: and  declare the same function. Note that the type (possibly cv-qualified) cannot be used in a parameter list otherwise:   and  are errors (although derived types, such as  can be used). In a template, only non-dependent void type can be used (a function taking a single parameter of type does not become a no-parameter function if instantiated with ).

An ellipsis may appear at the end of the parameter list; this declares a :

For compatibility with C89, an optional comma may appear before the ellipsis if the parameter list contains at least one parameter:

Parameter names declared in function declarations are usually for only self-documenting purposes. They are used (but remain optional) in function definitions.

The type of each function parameter in the parameter list is determined according to the following rules: @1@ First, and the declarator are combined as in any  to determine the type. @2@ If the type is "array of T" or "array of unknown bound of T", it is replaced by the type "pointer to T". @3@ If the type is a function type F, it is replaced by the type "pointer to F". @4@ Top-level cv-qualifiers are dropped from the parameter type (This adjustment only affects the function type, but doesn't modify the property of the parameter: and  declare the same function).

Because of these rules, the following function declarations declare exactly the same function:

The following declarations also declare exactly the same function:

An ambiguity arises in a parameter list when a type name is nested in parentheses. In this case, the choice is between the declaration of a parameter of type pointer to function and the declaration of a parameter with redundant parentheses around the identifier of the. The resolution is to consider the type name as a (which is the pointer to function type):

Parameter type cannot be a type that includes a reference or a pointer to array of unknown bound, including a multi-level pointers/arrays of such types, or a pointer to functions whose parameters are such types.

Function definition
A non-member function definition may appear at namespace scope only (there are no nested functions). A definition may also appear in the body of a. They have the following syntax:

where is one of the following

@1@ Regular function body. @2@ (which is a regular function body wrapped in a try/catch block). @3@ Explicitly deleted function definition. @4@ Explicitly defaulted function definition, only allowed for.

The function body is a (sequence of zero or more statements surrounded by a pair of curly braces), which is executed when the function call is made.

The parameter types, as well as the return type of a function definition cannot be (possibly cv-qualified). The completeness check is only made in the function body, which allows to return the class in which they are defined (or its enclosing class), even if it is incomplete at the point of definition (it is complete in the function body).

The parameters declared in the of a function definition are  within the body. If a parameter is not used in the function body, it does not need to be named (it's sufficient to use an abstract declarator):

Even though top-level on the parameters are discarded in function declarations, they modify the type of the parameter as visible in the body of a function:

{{rrev|since=c++11|

Deleted functions
If, instead of a function body, the special syntax is used, the function is defined as deleted. Any use of a deleted function is ill-formed (the program will not compile). This includes calls, both explicit (with a function call operator) and implicit (a call to deleted overloaded operator, special member function, allocation function etc), constructing a pointer or pointer-to-member to a deleted function, and even the use of a deleted function in an expression that is not. However, implicit of a non-pure virtual member function that happens to be deleted is allowed.

If the function is overloaded, takes place first, and the program is only ill-formed if the deleted function was selected:

The deleted definition of a function must be the first declaration in a translation unit: a previously-declared function cannot be redeclared as deleted:

User-provided functions
A function is user-provided if it is user-declared and not explicitly defaulted or deleted on its first declaration. A user-provided explicitly-defaulted function (i.e., explicitly defaulted after its first declaration) is defined at the point where it is explicitly defaulted; if such a function is implicitly defined as deleted, the program is ill-formed. Declaring a function as defaulted after its first declaration can provide efficient execution and concise definition while enabling a stable binary interface to an evolving code base.

__func__
Within the function body, the function-local predefined variable __func__ is defined as if by

This variable has block scope and static storage duration:

}}