Examine the file named DEFAULT.CPP for an example of the use of default parameters in C++.
int get_volume(int length, int width = 2, int height = 3);
int x = 10, y = 12, z = 15;
cout << "Some box data is " << get_volume(x, y, z) << "n";
cout << "Some box data is " << get_volume(x, y) << "n";
cout << "Some box data is " << get_volume(x) << "n";
cout << "Some box data is ";
cout << get_volume(x, 7) << "n";
cout << "Some box data is ";
cout << get_volume(5, 5, 5) << "n";
int get_volume(int length, int width, int height)
printf("%4d %4d %4d ", length, width, height);
return length * width * height;
// Result of execution
// 10 12 15Some box data is 1800
// 10 12 3Some box data is 360
// 10 2 3Some box data is 60
// Some box data is 10 7 3 210
// Some box data is 5 5 5 125
This program really looks strange since it contains default values for some of the parameters in the prototype, but these default values are very useful as we will see shortly.
This prototype says that the first parameter named length must be given for each call of this function because a default value is not supplied. The second parameter named width, however, is not required to be specified for each call, and if it is not specified, the value 2 will be used for the variable width within the function. Likewise, the third parameter is optional, and if it is not specified, the value of 3 will be used for height within the function.
In line 11 of this program, all three parameters are specified so there is nothing unusual about this call from any other function call we have made. Only two values are specified in line 12 however, so we will use the default value for the third parameter and the system acts as if we called it with get_value(x, y, 3) since the default value for the third value is 3. In line 13, we only specified one parameter which will be used for the first formal parameter, and the other two will be defaulted. The system will act as if we had called the function with get_volume(x, 2, 3). Note that the output from these three lines is reversed. This will be explained shortly.
There are a few rules which should be obvious but will be stated anyway. Once a parameter is given a default value in the list of formal parameters, all of the remaining must have default values also. It is not possible to leave a hole in the middle of the list, only the trailing values can be defaulted. Of course, the defaulted values must be of the correct types or a compiler error will be issued. The default values can be given in either the prototype or the function header, but not in both. If they are given in both places, the compiler must not only use the default value, but it must carefully check to see that both values are identical. This could further complicate an already very complicated problem, that of writing a C++ compiler.
As a matter of style, it is highly recommended that the default values be given in the prototype rather than in the function. The
reason will be obvious when we begin using object oriented programming techniques.
WHY IS THE OUTPUT SCRAMBLED?
When the compiler finds a cout statement, the complete line of code is initially scanned from right to left to evaluate any functions, then the data is output field by field from left to right. Therefore in line 11, get_value() is evaluated with its internal output displayed first. Then the fields of the cout are displayed from left to right with “Some box data is” displayed next. Finally, the result of the return from get_value() is output in int format, the type of the returned value. The end result is that the output is not in the expected order when lines 11 through 13 are executed. (The output is not what you would intuitively expect to happen so appears to be a deficiency in the language. A call to Borland International, the writers of Turbo C++ and Borland C++, verified that this is operating correctly.)
Lines 15 through 18 are similar to any two of the lines of code in lines 11 through 13, but are each separated into two lines so the output is in the expected order.
Be sure to compile and execute DEFAULT.CPP after you understand it. Note that the funny output order will appear again later in this tutorial