< previous page page_63 next page >

Page 63
10905-0063a.gif
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:
long Area(int, int);
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

d5ef64f4d3250b96ba5c07ca5bbc2f56.gif
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:
d5ef64f4d3250b96ba5c07ca5bbc2f56.gif
continues

 
< previous page page_63 next page >

If you like this book, buy it!