Function declarations and definitions

The distinction between a function declaration and function definition is similar to that of a data declaration and definition. The declaration establishes the names and characteristics of a function but does not allocate storage for it, while the definition specifies the body for a function, associates an identifier with the function, and allocates storage for it. Thus, the identifiers declared in this example:

float square(float x);

do not allocate storage.

The function definition contains a function declaration and the body of a function. The body is a block of statements that perform the work of the function. The identifiers declared in this example allocate storage; they are both declarations and definitions.

float square(float x) 
{ return x*x; }

A function can be declared several times in a program, but all declarations for a given function must be compatible; that is, the return type is the same and the parameters have the same type. However, a function can only have one definition. Declarations are typically placed in header files, while definitions appear in source files.



[ Top of Page | Previous Page | Next Page | Contents | Index ]