Sunday 21 April 2013

Default Values for Function Parameters

Functions can be called with fewer actual parameters than formal parameters. The
unspecified parameters are given default values.
void fred(int one, float two = 1.23, char three = 'c')
{}
The arguments with default values must be the last parameters in the parameter list.
In this example, fred can then be called with 1, 2 or 3 parameters, e.g.
fred(6,7.2,'z');
fred(6,7.2);
fred(6);
When declaring functions that take parameters with default values, subsequent declarations
cannot specify the default values already specified in earlier declarations, but they can add
new default values. When declaring a function before use, specify the default values in the
first declaration, e.g.
void fred(int one, float two = 1.23, char three = 'c');
int main()
{
fred(6, 7.2);
}
void fred(int one, float two, char three)
{}

No comments:

Post a Comment