The parameter list of a function signature can specify a default value associated with a parameter. Only the last n consecutive parameters can have a default value, the first n consecutive parameters can have no default. In both cases, n can be zero. Defaults must be specified at the time of declaration of the function, or the function can be declared and defined simultaneously, as it is with inline functions. An example is:
void df(int, float, int = 12, char = 'A');
This can be called in one of three ways:
df(10, 2.2); // defaults used for parameters three and four df(10, 2.2, 32); // default only used for last one df(10, 2.2, 32, 'B'); // both defaults are over-ridden
Default arguments are a kind of function overloading.
Defaults can only be specified once. If a function is declared and then defined later, the definition must not mention the defaults:
void f(int = 10); ... void f(int x) { ... }
Copyright © 2003 Roger Hartley