Wrox Press C++ Tutorial


Initializing Function Parameters

With all the functions we have used up to now, we have had to take care to provide an argument corresponding to each parameter in a function call. It can be quite handy to be able to omit one or more arguments in a function call, and have some default values, for the arguments that you leave out, supplied automatically. You can arrange this by initializing the parameters to a function in its prototype.

For example, let's suppose that we write a function to display a message, where the message to be displayed is to be passed as an argument. Here is the definition of such a function:

void showit(char* message)
{
   cout << endl
        << message;
   return;
}

We can initialize the parameter to this function by specifying the initializing string value in the function prototype, as follows:

void showit(char* message = "Something is wrong.");

Here, the parameter message is initialized with the string shown. If you've initialized a parameter to a function in the prototype, and if you leave out that argument when you call the function, the initializing value is used in the call.

Try It Out - Omitting Function Arguments

Leaving out the function argument when you call the function will execute it with the default value. If you supply the argument, it will replace the default value. We can use the previous function to output a variety of messages.

//Ex5_03.cpp
// Omitting function arguments
#include <iostream>
using namespace std;

void showit(char* = "Something is wrong.");

int main()
{
   char* mymess = "The end of the world is nigh.";
   showit();                   // Display the basic message
   // Display an alternative
   showit("Something is terribly wrong!");
   showit();                   // Display the default again
   showit(mymess);             // Display a predefined message
   cout << endl;
   return 0;
}

void showit(char* message)
{
   cout << endl
        << message;
   return;
}

If you execute this example, it will produce the following apocalyptic output:

How It Works

As you can see, we get the default message specified in the function prototype whenever the argument is left out. Otherwise, the function behaves normally.

If you have a function with several arguments, you can provide initial values for as many of them as you like. If you want to omit more than one argument to take advantage of a default value, all arguments to the right of the leftmost argument that you omit must also be left out. For example, if you have this function,

int do_it(long arg1=10, long arg2=20, 
          long arg3=30, long arg4=40);

and you want to omit one argument in a call to it, you can omit only the last one, arg4. If you want to omit arg3, you must also omit arg4. If you omit arg2, arg3 and arg4 must also be omitted, and if you want to use the default value for arg1, you have to omit all of the arguments in the function call.

You can conclude from this that you need to put the arguments which have default values in the function prototype together in sequence at the end of the parameter list, with the argument most likely to be omitted appearing last.


© 1998 Wrox Press