다음을 통해 공유


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
} 

The parts of the definition are:

  • Declaration specifiers, as described in Function Declarations.

  • The declarator. See below.

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

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

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

The form of the declarator is:

  • Optional pointer or reference operators modifying 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 using the scope resolution operator.

  • The argument declaration list enclosed in parentheses ().

  • For constructors, an optional constructor initializer (see below).

See the comments in Function Declarations on functions returning function pointers for information on the form of the declarator in such cases.

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

Function Definition Parts

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.)

See Also

Reference

Declarators