< Previous | Contents | Next >

Specifying Default Arguments

The function askNumber() has two parametershigh and low. You can tell this from the function prototype:

int askNumber(int high, int low = 1);

Notice that the second parameter, low, looks like its assigned a value. In a way, it is. The 1 is a default argument meaning that if a value isnt passed to low when the function is called, low is assigned 1. You specify default arguments by using = followed by a value after a parameter name.

Tra p

image

Once you specify a default argument in a list of parameters, you must specify default arguments for all remaining parameters. So the following prototype is valid:

void setDisplay(int height, int width, int depth = 32, bool fullScreen = true);

while this one is illegal:

void setDisplay(int width, int height, int depth = 32, bool fullScreen);

image


By the way, you dont repeat the default argument in the function definition, as you can see in the function definition of askNumber().

int askNumber(int high, int low)