|
|
In C++, you may assign a function parameter a default value, which will be used automatically when no corresponding argument is specified when the function is called. The default value is specified in a manner syntactically similar to a variable initialization. For example, this function assigns its two parameters default values:
void myfunc(int a = 0, int b = 10) { // ...
Given the default arguments, myfunc( ) can be legally called in these
three ways:
myfunc(); // a defaults to 0; b defaults to 10 myfunc(-1); // a is passed -1; b defaults to 10 myfunc(-1, 99); // a is passed -1; b is passed 99
When you create functions that have default arguments, you must specify the default values only once: either in the function prototype or in its definition. (You cannot specify them each place, even if you use the same values.) Generally, default values are specified in the prototype.
When giving a function default arguments, remember that you must specify all nondefaulting arguments first. Once you begin to specify default arguments, there may be no intervening nondefaulting ones.
Default arguments are not supported by C.
|
|