C++ Function Definitions

Function definitions differ from function declarations in that they supply function bodies—the code that makes up the function. The form of a function definition is:

decl-specifiers declarator [cv-qualifers] [exception-specification]
   // function body
decl-specifiers declarator [cv-qualifers] =default;

decl-specifiers declarator [cv-qualifers] =delete;

The parts of the definition are:

  • Declaration specifiers, as described in Function Declarations.

  • The declarator (described later in this article).

  • An optional const or volatile qualifier. In this context, const may only be used for class members, to indicate that the function will not modify data members of the class.

  • Exception specification that describes what exceptions the function may throw. See Exception Specifications.

  • Function-body, consisting of statements enclosed in curly braces {}.

  • =default; in the case of an explicitly-defaulted definition.

  • =delete; in the case of a deleted definition.

The form of the declarator is:

  • Optional pointer or reference operators that modify the return type.

  • An optional Microsoft-specific modifier. See Microsoft-Specific Modifiers.

  • The name of the function. If the function is a member of a class or struct, the name may be qualified by using the scope-resolution operator.

  • The argument declaration list enclosed in parentheses ().

  • For constructors, an optional constructor initializer (described later in this article).

For information about the form of the declarator in such cases, see the comments in Function Declarations about functions that return function pointers.

The formal arguments declared in the argument declaration list are in the scope of the function body.

The following figure shows the parts of a function definition. The shaded area is the function body.

Parts of a Function Definition

Parts of a function definition

The constructor initializer element of the syntax is used only in constructors. Its purpose is to allow initialization of base classes and contained objects. For more information about use of the constructor initializer, see Initializing Bases and Members.

An explicitly-defaulted definition can only be declared for a special member function. When a special member function is explicitly defaulted, the implementation defines it as if it had an implicit definition, except that it may be non-inline (an implicitly-declared special member function is always inline). For more information about defaulted functions, see the "Defaulted and deleted functions" section in Support For C++11 Features (Modern C++).

A deleted definition, also known as a deleted function, is implicitly inline. A program that refers to a deleted function either explicitly or implicitly—other than to declare it—is ill-formed. For more information about deleted functions, see the "Defaulted and deleted functions" section in Support For C++11 Features (Modern C++).

Community Additions