8 Default parameter values

Both C and C++ supports default parameter values. In other words, you can define a function prototype as follows:

void xyz(int x, int y = 6);

Then, when the function is invoked, both of the following invocations are valid:

xyz(1,2); // explicit value for y
xyz(3); // default value of 6 for y

Note that parameters with default values must be the last ones.

Generally speaking, I prefer not to specify default values for parameters. If I use a particular value for a particular parameter very often, I'd define another function (inline, perhaps) as follows:

inline void lazyXyz(int x)
{
  xyz(x,6); // no need to use default param value
}

Can you think of reasons why parameter default value may not be such a great feature?



Copyright © 2006-08-29 by Tak Auyeung