Previous Table of Contents Next


Listing 5.4 Demonstrates Multiple Return Statements


1:     // Listing 5.4 - demonstrates multiple return
2:     // statements
3:
4:     #include <iostream.h>
5:
6:     int Doubler(int AmountToDouble);
7:
8:     int main()
9:     {
10:
11:         int result = 0;
12:         int input;
13:
14:         cout << “Enter a number between 0 and 10,000 to double: ”;
15:         cin >> input;
16:
17:         cout << “\nBefore doubler is called... ”;
18:         cout << “\ninput: ” << input << “ doubled: ” << result << “\n”;
19:
20:         result = Doubler(input);
21:
22:         cout << “\nBack from Doubler...\n”;
23:         cout << “\ninput: ” << input << “   doubled: ” << result << “\n”;
24:
25:
26:         return 0;
27:    }
28:
29:    int Doubler(int original)
30:    {
31:         if (original <= 10000)
32:              return original * 2;
33:         else
34:              return -1;
35:         cout << “You can’t get here!\n”;
36:    }

Enter a number between 0 and 10,000 to double: 9000
Before doubler is called...
input: 9000 doubled: 0
Back from doubler...
input: 9000 doubled: 18000

Enter a number between 0 and 10,000 to double: 11000
Before doubler is called...
input: 11000 doubled: 0
Back from doubler...
input: 11000 doubled: -1

A number is requested on lines 14 and 15 and printed on line 18, along with the local variable result. The function Doubler() is called on line 20, and the input value is passed as a parameter. The result will be assigned to the local variable result, and the values will be reprinted on lines 22 and 23.

On line 31, in the function Doubler(), the parameter is tested to see whether it is greater than 10,000. If it is not, the function returns twice the original number. If it is greater than 10,000, the function returns -1 as an error value.

The statement on line 35 is never reached, because whether or not the value is greater than 10,000, the function returns before it gets to line 35, on either line 32 or line 34. A good compiler will warn that this statement cannot be executed, and a good programmer will take it out!

Default Parameters

For every parameter you declare in a function prototype and definition, the calling function must pass in a value. The value passed in must be of the declared type. Thus, if you have a function declared as

long myFunction(int);

the function must, in fact, take an integer variable. If the function definition differs, or if you fail to pass in an integer, you will get a compiler error.

The one exception to this rule is if the function prototype declares a default value for the parameter. A default value is a value to use if none is supplied. The preceding declaration could be rewritten as

long myFunction (int x = 50);

This prototype says “myFunction() returns a long and takes an integer parameter. If an argument is not supplied, use the default value of 50.” Because parameter names are not required in function prototypes, this declaration could have been written as

long myFunction (int = 50);

The function definition is not changed by declaring a default parameter. The function definition header for this function would be

long myFunction (int x)

If the calling function did not include a parameter, the compiler would fill x with the default value of 50. The name of the default parameter in the prototype need not be the same as the name in the function header; the default value is assigned by position, not name.

Any or all the function’s parameters can be assigned default values. The one restriction is this: If any of the parameters does not have a default value, no previous parameter may have a default value.

If the function prototype looks like

long myFunction (int Param1, int Param2, int Param3);

you can assign a default value to Param2 only if you have assigned a default value to Param3. You can assign a default value to Param1 only if you’ve assigned default values to both Param2 and Param3. Listing 5.5 demonstrates the use of default values.

Listing 5.5 Demonstrates Default Parameter Values


1:   // Listing 5.5 - demonstrates use
2:   // of default parameter values
3:
4:   #include <iostream.h>
5:
6:   int AreaCube(int length, int width = 25, int height = 1);
7:
8:   int main()
9:   {
10:       int length = 100;
11:       int width = 50;
12:       int height = 2;
13:       int area;
14:
15:       area = AreaCube(length, width, height);
16:       cout << “First area equals: ” << area << “\n”;
17:
18:       area = AreaCube(length, width);
19:       cout << “Second time area equals: ” << area << “\n”;
20:
21:       area = AreaCube(length);
22:       cout << “Third time area equals: ” << area << “\n”;
23:       return 0;
24:  }
25:
26:  AreaCube(int length, int width, int height)
27:  {
28:
29:       return (length * width * height);
30:  }

First area equals: 10000
Second time area equals: 5000
Third time area equals: 2500

On line 6, the AreaCube() prototype specifies that the AreaCube() function takes three integer parameters. The last two have default values.

This function computes the area of the cube whose dimensions are passed in. If no width is passed in, a width of 25 is used and a height of 1 is used. If the width, but not the height is passed in, a height of 1 is used. It is not possible to pass in the height without passing in a width.

On lines 10–12, the dimensions length, height, and width are initialized, and they are passed to the AreaCube() function on line 15. The values are computed, and the result is printed on line 16.


Previous Table of Contents Next