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 4 using std::cout; 5 using std::endl; 6 7 // function prototype that specifies default arguments 8 int boxVolume( int length = 1, int width = 1, 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 return 0; // indicates successful termination 28 } // end main 29 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
|
Common Programming Error 6.18
It is a compilation error to specify default arguments in both a function's prototype and header. |
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. |
Introduction to Computers, the Internet and World Wide Web
Introduction to C++ Programming
Introduction to Classes and Objects
Control Statements: Part 1
Control Statements: Part 2
Functions and an Introduction to Recursion
Arrays and Vectors
Pointers and Pointer-Based Strings
Classes: A Deeper Look, Part 1
Classes: A Deeper Look, Part 2
Operator Overloading; String and Array Objects
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Polymorphism
Templates
Stream Input/Output
Exception Handling
File Processing
Class string and String Stream Processing
Web Programming
Searching and Sorting
Data Structures
Bits, Characters, C-Strings and structs
Standard Template Library (STL)
Other Topics
Appendix A. Operator Precedence and Associativity Chart
Appendix B. ASCII Character Set
Appendix C. Fundamental Types
Appendix D. Number Systems
Appendix E. C Legacy Code Topics
Appendix F. Preprocessor
Appendix G. ATM Case Study Code
Appendix H. UML 2: Additional Diagram Types
Appendix I. C++ Internet and Web Resources
Appendix J. Introduction to XHTML
Appendix K. XHTML Special Characters
Appendix L. Using the Visual Studio .NET Debugger
Appendix M. Using the GNU C++ Debugger
Bibliography