Default Parameters in C++ Functions
We've seen how function overloading gives the user
several functions with the same name that (persumably)
perform the same (or similar) task with different data types.
A similar, but different, idea is to provide a single function
that can be called with a different number of parameters.
Consider the function InitArray( ) below that
initializes the given array of integers to the user-specified value.
void InitArray (int array[], int size, int value)
{
for (int i = 0; i < size; i++)
array[i] = value;
}
which might be used in the code below
void InitArray( int array[], int size, int value);
int main ( )
{
int a[10], b[20], c[5];
InitArray (a, 10, 0);
InitArray (b, 20, 0);
InitArray (c, 5, -1);
// the rest of main here
}
In most of our experience, we use arrays of integers
as counters or sums and would like them to be intialized
to zero. We can define InitArray( ) above to
use zero as the default argument value of the parameter
named "value" by a minor change to the function's prototype
void InitArray( int array[], int size, int value = 0);
The code in main( ) above still works exactly as written,
but could also be written as
int main ( )
{
int a[10], b[20], c[5];
InitArray (a, 10);
InitArray (b, 20);
InitArray (c, 5, -1);
// the rest of main here
}
Note that the first two calls to InitArray( ) have just
two parameters, whereas the last call has three parameters.
Rules for default arguments:
- The default argument value is given in the function prototype,
not in the function definition
- There may be more than one default argument, but all default
argument positions must be in the rightmost positions. This is
illustrated in Display 4.8 (pg 160 of the text) which is shown below.
- Default arguments can only be use with call-by-value parameters.
(They make no sense for call-by-reference parameters).
Note also, that anything you can do with default arguments can
also be done with function overloading. As an exercise, consider how
you would use function overloading in the InitArray() example
above.
Display 4.8 from the text (below) shows a function showVolume() with two
default argument values. Note that showVolume() can be called in
three different ways. When showVolume() is called with missing arguments,
the missing arguments are assumed to be the right most arguments.
//---------------------------------
// File: 04-08.cpp
// Date: 8/20/03
// Author: D. Frey
// Section: n/a
// EMail: frey@cs.umbc.edu
//
// This is a modified version of example 04-08
// from the text, page 160 which is an example
// of using default parameters
//---------------------------------------------------
#include
using namespace std;
// prototypes for functions in this file
void ShowVolume(int length, int width = 1, int height = 1);
int main( )
{
ShowVolume( 4, 6, 2 );
ShowVolume( 4, 6 );
ShowVolume( 4 );
return 0;
}
//-------------------------------------------
// ShowVolume ( )
// PreConditions:
// params should all be nonnegative
// PostConditions
// Returns the volume of a box.
// If no height is given, the height is assumed to be 1.
// If neither height nor width are given, both are assumed to be 1.
//---------------------------------------------
void ShowVolume(int length, int width, int height)
{
cout << "Volume of a box with \n"
<< "Length = " << length << ", Width = " << width << endl
<< "and Height = " << height
<< " is " << length * width * height << endl;
}
Last Modified: Monday, 28-Aug-2006 10:15:59 EDT