|
|
|
|
|
|
|
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 |
|
|
|
|
|