C++ Function Definitions

Function definitions differ from function declarations in that they supply function bodies — the code that makes up the function.

Syntax

function-definition :

decl-specifiersopt declarator ctor-initializeropt fct-body

fct-body :

compound-statement

As discussed in Functions, the form of the declarator in the syntax is:

dname ( argument-declaration-list ) cv-mod-listopt

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

Figure 7.3 shows the parts of a function definition. The shaded area is the function body.

Figure 7.3   Parts of a Function Definition

The cv-mod-list element of the declarator syntax specifies how the this pointer is to be treated; it is only for use with class member functions.

The ctor-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 ctor-initializer, see Initializing Bases and Members in Chapter 11.)