2.5 Default Arguments




Default arguments are formal parameters that are given a value (the default value) to use when the caller does not supply an actual parameter value. Of course, actual parameter values, when given, override the default values.

The syntax and and utility of default arguments is illustrated by the following example based on the Frame class.

    class Frame {
    private:
	...
    public:
	...
	void Resize (int width = 100, int height = 150);
	...
    };
In this example, the width and height arguments are both given default values. To see the effect of these default values consider the following use of the Resize method:

     Frame window (100, 100, 300, 400);

	...

     window.Resize(200, 250);		// case 1
     window.Resize(200);		// case 2
     window.Resize();			// case 3

In the first case the window is resized to be 200 X 250: since both parameters are given the default values are ignored. In the second case, the Frame object is resized to be 200 X 150: the default value of 150 is used for the absent second (height) argument while the supplied 200 is used for the first (width) argument. In the third case, the window is resized to be 100 X 200 since both default values are used.

Default values can also be used with constructors. For example the Frame class could be redefined as follows:

    class Frame {
    private:
	...
    public:
        Frame( char *name, int initXcoord =  50, int initYcoord =  50,
                           int initWidth  = 100, int initHeight = 100 );
	...
    };

This single constructor can be used in any of the following ways:

	Frame display1("Fully Specified", 200, 200, 300, 350);
        Frame display2("No Height",      200, 200, 300);
        Frame display3("Position Only",  200, 200);
        Frame display4("No YCoordinate", 200)
        Frame display5("Name Only");

The Frame display1 is given a name and will be located at position (200,200) with a shape of 300 by 350. The Frame display2 is like display1 except that it has a default height of 100 as specified by the default value for initHeight. The Frame display3 is given a name and will be located at position (200,200) and it will have a shape of 100 by 100 as determined by the default values for initWidth and initHeight. The Frame display 4 is similar to display3 except that it has a different name and will be located at (200,50), the y coordinate of 50 being determined by the default value for the parameter initYcoord. Finally, display5 will have the name indicated and the default values will determine its position - (50,50) - and it shape - 100 by 100.

Notice that as with overloaded methods, the use of default parameters give the caller of a method added flexibility in how to invoke the method. The caller need only specify those parameters that are essential and allow the default values to apply for all other parameters. Callers who need more exact control of the methods arguments can override the default values by providing their own values.

Default values can be used when:

Examples of common or safe default values are an integer "counter" whose default initial value is zero or a text value whose initial contents is blank or null. A variation of the Frame class is an example of using unreasonable default values. In this variation the default values for the x coordinate, the y coordinate, the height, and the width are -1, clearly unreasonable values. The Frame constructor when confronted with (some of) these default values uses an internal algorithm to determine what values to use for each defaulted argument. The algorithm may, for example, attempt to minimize the overlap of this window with other windows on the screen.

The rule for specifying default values is that a argument without a default value can not occur after (to the right of) an argument with a default value. The following example illustrates why this rule is needed:

   class Cube {
   public:
	...
	Resize( int height = 10, int width = 20, int depth);
	...
   };
Notice that this violates the rule for default arguments because the argument depth has no default and occurs after width which has a default value. Consider the following code that uses the Resize method:

     Cube cube;
     ...
     cube.Resize(30, 50);
This invocation of Resize could reasonably mean any one of three things:

     cube.Resize(10, 30, 50);		// uses height default
     cube.Resize(30, 20, 50);		// uses width default
     cube.Resize(30, 50);		// ERROR - no depth argument 
This ambiguous situation is avoided (both for the benefit of the compiler and the programmer) by the rule given above.


Next Stop


Exercises

  1. For the Frame class write a single constructor with default values that could replace its four overloaded constructors.

  2. Can the overloaded Resize method in the Frame class be combined into a single method using default values? Explain.

  3. Write a definition for the MoveTo method in the Frame class using default values. Explain and justify the default values you selected.

  4. Write a definition for the Resize(int,int) method in the Frame class using default values. Explain and justify the default values you selected.

  5. Write a definition for the Resize(float) method in the Frame class using default values. Explain and justify the default values you selected.

  6. Is is possible to have both of the following methods in the same class: Resize(int height = 100, int width = 100); and Resize(); ?

Last Updated: May 21, 1996 / kafura@cs.vt.edu