It’s common for a program to invoke a function repeatedly with the same argument value for a particular parameter. In such cases, you can specify that such a parameter has a default argument, i.e., a default value to be passed to that parameter. When a program omits an argument for a parameter with a default argument in a function call, the compiler rewrites the function call and inserts the default value of that argument.
Default arguments must be the rightmost (trailing) arguments in a function’s parameter list. When calling a function with two or more default arguments, if an omitted argument is not the rightmost argument in the argument list, then all arguments to the right of that argument also must be omitted. Default arguments must be specified with the first occurrence of the function name—typically, in the function prototype. If the function prototype is omitted because the function definition also serves as the prototype, then the default arguments should be specified in the function header. Default values can be any expression, including constants, global variables or function calls. Default arguments also can be used with inline
functions.
Figure 6.20 demonstrates using default arguments to calculate a box’s volume. The function prototype for boxVolume
(line 7) specifies that all three parameters have been given default values of 1
. We provided variable names in the function prototype for readability. As always, variable names are not required in function prototypes.
1 // Fig. 6.20: fig06_20.cpp
2 // Using default arguments.
3 #include <iostream>
4 using namespace std;
5
6 // function prototype that specifies default arguments
7 unsigned int boxVolume( unsigned int length = 1, unsigned int width = 1,
8 unsigned int height = 1 );
9
10 int main()
11 {
12 // no arguments--use default values for all dimensions
13 cout << "The default box volume is: " << boxVolume();
14
15 // specify length; default width and height
16 cout << "
The volume of a box with length 10,
"
17 << "width 1 and height 1 is: " << boxVolume( 10 );
18
19 // specify length and width; default height
20 cout << "
The volume of a box with length 10,
"
21 << "width 5 and height 1 is: " << boxVolume( 10, 5 );
22
23 // specify all arguments
24 cout << "
The volume of a box with length 10,
"
25 << "width 5 and height 2 is: " << boxVolume( 10, 5, 2 )
26 << endl;
27 } // end main
28
29 // function boxVolume calculates the volume of a box
30 unsigned int boxVolume( unsigned int length, unsigned int width,
31 unsigned int height )
32 {
33 return length * width * height;
34 } // end function boxVolume
The default box volume is: 1
The volume of a box with length 10,
width 1 and height 1 is: 10
The volume of a box with length 10,
width 5 and height 1 is: 50
The volume of a box with length 10,
width 5 and height 2 is: 100
The first call to boxVolume
(line 13) specifies no arguments, thus using all three default values of 1. The second call (line 17) passes only a length
argument, thus using default values of 1 for the width
and height
arguments. The third call (line 21) passes arguments for only length
and width
, thus using a default value of 1 for the height
argument. The last call (line 25) passes arguments for length
, width
and height
, thus using no default values. Any arguments passed to the function explicitly are assigned to the function’s parameters from left to right. Therefore, when boxVolume
receives one argument, the function assigns the value of that argument to its length
parameter (i.e., the leftmost parameter in the parameter list). When boxVolume
receives two arguments, the function assigns the values of those arguments to its length
and width
parameters in that order. Finally, when boxVolume
receives all three arguments, the function assigns the values of those arguments to its length
, width
and height
parameters, respectively.
3.142.166.31