Default
Function Arguments
C++
allows a function to assign a parameter a default value when no argument corresponding
to that parameter is specified in a call to that function. The default value is
specified in a manner syntactically similar to a variable initialization. For
example, this declares myfunc() as taking one double argument
with a default value of 0.0:
void
myfunc(double d = 0.0)
{
//
...
}
Now, myfunc()
can be called one of two ways, as the following examples show:
myfunc(198.234);
// pass an explicit value
myfunc();
// let function use default
The
first call passes the value 198.234 to d. The second call automatically
gives d the default value zero.
One
reason that default arguments are included in C++ is because they provide another
method for the programmer to manage greater complexity. To handle the widest
variety of situations, quite frequently a function contains more parameters
than are required for its most common usage. Thus, when the default arguments
apply, you need specify only the arguments that are meaningful to the exact
situation, not all those needed by the most general case.
If you supply a default parameter, all
other parameters following that parameter in the parameter list must have
defaults too.
Using default arguments is essentially a shorthand form of
function overloading.
To give a parameter a default argument, simply follow that
parameter with an equal sign and the value you want it to default to if no
corresponding argument is present when the function is called. For example,
this function gives two parameters default values of 0:
void
f(int a=0, int b=0);
Notice that this syntax is similar to variable initialisation.
This function can now be called three different ways:
• It can be called with both arguments specified.
• It can be called with only the first argument specified (in this
case b will default to 0).
• It
can be called with no arguments (both a and b default to 0).
That
is the following calls to the function f are valid,
f( ); // a
and b default to 0
f(10); // a
is 10 and b defaults to 0
f(10, 99); // a
is 10 and b is 99
A default parameter (also called an optional parameter or a default argument) is a function parameter that has a default value provided to it. If the user does not supply a value for this parameter, the default value will be used. If the user does supply a value for the default parameter, the user-supplied value is used instead of the default value.
Rules to implement the concept of Default Arguments:
1. When
you create a function that has one or more default arguments, those
arguments must be specified only once: either in the function's
prototype or in the function's definition if the definition precedes the
function's first use.
2. The defaults cannot be specified in both the prototype
and the definition. This rule applies even if you simply duplicate the same
defaults.
3. All
default parameters must be to the right of any parameters that don't have
defaults. Further,
once you begin define default parameters, you cannot specify any
parameters that have no defaults.
4. Default
arguments must be constants or global variables. They cannot be local
variables or other parameters.
5. Default
arguments often provide a simple alternative to function overloading. Of course
there are many situations in which function overloading is required.
6. It is not only legal to give constructor
functions default arguments, it is also common. Many times a constructor is overloaded simply to
allow both initialised and uninitialised objects to be created.
No comments:
Post a Comment