Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) function declaration

 

A function declaration states what a function needs and returns with giving its arguments proper names (without these names, it would be a called a function prototype). A function declaration can be seen as a promise to the compiler that a certain function exists and will be found by the linker.

 

Although a function declaration tells nothing about the implementation of the function, an advanced programmer will assume a lot from it! And sometimes, a function declaration is all you will be allowed to see. View Exercise #2: correct function declarations to learn about correct function declarations.

 

Where a function declaration tells nothing about the implementation of the function, a function definition consists of a function declaration including its implementation.

 

Function declarations are commonly found in header files (.h), where function definitions in implementation (.cpp) files.

 

 

 

 

 

Examples

 

Below I give some function declarations and how to read them.

 

void SayHello();

 

The function SayHello lets the computer say hello in a certain unspecified way.

 

void Swap(int& a, int& b);

 

The function Swap uses two non-copied values (due to the reference) and modifies (due to the omission of const) them both. It probably swaps the values of 'a' and 'b'. Swap does nothing more, because it has no (void) return type.

 

int GetRows(const Database& d);

 

The function GetRows obtains the number of rows (the const int return type) from a certain Database. It needs an existing Database (it uses a reference, instead of a pointer that can be null) and does not copy (due to the reference) nor modify (due to the const) it.

 

int Sum(const std::vector<int>& v);

 

The function Sum obtains the sum of values (the const int return type) from a certain std::vector. It needs an existing std::vector (it uses a reference, instead of a pointer that can be null) and does not copy (due to the reference) nor modify (due to the const) it.

 

void MeanAndStdDev(const std::vector<double>& v, double& mean, double& stdDev);

 

The function MeanAndStdDev uses an existing std::vector (it uses a reference, instead of a pointer that can be null) and does not copy (due to the reference) nor modify (due to the const) it. MeanAndStdDev does not return the mean and standard deviation by its return type (void), but by using two references. Therefore, the caller of MeanAndStdDev must first create two doubles, which will store the mean and standard deviation after the call to MeanAndStdDev.

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict