Barry Schwarz said:
I thought the purpose of a default value for a function argument was
to assign a value when the calling statement didn't. (That seems to
be the way it works in those languages which let you do this.) The
code you have for func2 could be just as easily replaced with
int func2(int a)
{
int flags = 0;
}
rendering func unnecessary.
The intent is that the body of func2 actually uses the value of flags.
If I want to use a non-zero flags value (say, if I want to specify
some unusual behavior), I can call func2:
func2(42, 0x40);
If I want to use a zero flags value, the "func" wrapper gives me a
shortcut:
func(42);
which is equivalent to
func(42, 0);
There are examples of this in the standard library: printf is
effectively a wrapper for fprintf, providing a default value (stdout)
for the first parameter.
But we still have the question - other than variadic functions, how do
you not provide an argument in the calling statement? Would the
language have to be expanded to accept consecutive commas in the
argument list? Of course, the missing argument would be replaced by
the default value from the prototype.
There are several possibilities. The simplest is to require that any
optional parameters (i.e., parameters with default values) must be
declared at the end of the parameter list. Then a call can omit one
or more trailing arguments, and the corresponding parameters take
their default values.
Or, as you suggest, you could accept consecutive commas -- but I
personally dislike this, since it makes calls too difficult to read:
foo("some string", 42, , , , '$');
The '$' is, let's see, [count commas] the 6th argument, so it
corresponds to, um, [count commas in declaration] this parameter.
Yet another possibility is to add named parameter associations, so
that arguments don't have to be given in the specified order, and you
can skip parameters without using consecutive commas. (Ada does
this.)
I'm doubtful that this feature will be added to standard C any time
soon.