|
|
|
|
|
|
|
FIGURE 5.2
Parts of a function prototype. |
|
|
|
|
|
|
|
|
The function prototype and the function definition must agree exactly about the return type, the name, and the parameter list. If they do not agree, you will get a compile-time error. Note, however, that the function prototype does not need to contain the names of the parameters, just their types. A prototype that looks like this is perfectly legal: |
|
|
|
|
|
|
|
|
This prototype declares a function named Area() that returns a long and that has two parameters, both integers. Although this is legal, it is not a good idea. Adding parameter names makes your prototype clearer. The same function with named parameters might be: |
|
|
|
|
|
|
|
|
long Area(int length, int width); |
|
|
|
|
|
|
|
|
It is now obvious what this function does and what the parameters are. |
|
|
|
|
|
|
|
|
Note that all functions have a return type. Listing 5.1 demonstrates a program that includes a function prototype for the Area() function. |
|
|
|
|
|
|
|
|
LISTING 5.1 SHOWS A FUNCTION DECLARATION AND THE DEFINITION AND USE OF THAT FUNCTION |
|
|
|
 |
|
|
|
|
1: // Listing 5.1 - demonstrates the use of function prototypes
2:
3: #include <iostream.h>
4: int FindArea(int length, int width); //function prototype
5:
6: int main()
7: {
8: int lengthOfYard;
9: int widthOfYard;
10: int areaOfYard;
11:
12: cout << \nHow wide is your yard? ;
13: cin >> widthOfYard;
14: cout << \nHow long is your yard? ;
15: cin >> lengthOfYard;
16:
17: areaOfYard= FindArea(lengthOfYard,widthOfYard);
18: |
|
|
|
 |
|
|
|
|
continues |
|
|
|
|
|