Main Page

Previous Page
Next Page

[Page 280]

6.15. Default Arguments

It is not uncommon for a program to invoke a function repeatedly with the same argument value for a particular parameter. In such cases, the programmer 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 to be passed as an argument to the function call.

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 should be specified with the first occurrence of the function nametypically, 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.22 demonstrates using default arguments in calculating the volume of a box. The function prototype for boxVolume (line 8) specifies that all three parameters have been given default values of 1. Note that we provided variable names in the function prototype for readability. As always, variable names are not required in function prototypes.

Figure 6.22. Default arguments to a function.
(This item is displayed on pages 280 - 281 in the print version)

 1  // Fig. 6.22: fig06_22.cpp
 2  // Using default arguments.
 3  #include <iostream>
 4  using std::cout;
 5  using std::endl;
 7  // function prototype that specifies default arguments
 8  int boxVolume( int length = 1, int width = 1, int height = 1 );
10  int main()
11  {
12     // no arguments--use default values for all dimensions
13     cout << "The default box volume is: " << boxVolume();
15     // specify length; default width and height
16     cout << "\n\nThe volume of a box with length 10,\n"
17        << "width 1 and height 1 is: " << boxVolume( 10 );
19     // specify length and width; default height
20     cout << "\n\nThe volume of a box with length 10,\n"
21        << "width 5 and height 1 is: " << boxVolume( 10, 5 );
23     // specify all arguments
24     cout << "\n\nThe volume of a box with length 10,\n"
25        << "width 5 and height 2 is: " << boxVolume( 10, 5, 2 )
26        << endl;
27     return 0; // indicates successful termination
28  } // end main
30  // function boxVolume calculates the volume of a box
31  int boxVolume( int length, int width, 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

Common Programming Error 6.18

It is a compilation error to specify default arguments in both a function's prototype and header.

[Page 281]

The first call to boxVolume (line 13) specifies no arguments, thus using all three default values of 1. The second call (line 17) passes a length argument, thus using default values of 1 for the width and height arguments. The third call (line 21) passes arguments for 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. Note that 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.

Good Programming Practice 6.6

Using default arguments can simplify writing function calls. However, some programmers feel that explicitly specifying all arguments is clearer.

Software Engineering Observation 6.16

If the default values for a function change, all client code using the function must be recompiled.

Common Programming Error 6.19

Specifying and attempting to use a default argument that is not a rightmost (trailing) argument (while not simultaneously defaulting all the rightmost arguments) is a syntax error.

Previous Page
Next Page