|
|
 |
|
|
|
|
19: cout << \nYour yard is ;
20: cout << areaOfYard;
21: cout << square feet\n\n;
22: return 0;
23: }
24:
25: int FindArea(int 1, int w)
26: {
27: return 1 * w;
28: } |
|
|
|
|
|
|
|
|
How wide is your yard? 100
How long is your yard? 200
Your yard is 20000 square feet |
|
|
|
|
|
|
|
|
Analysis: The prototype for the FindArea() function is on line 4. Compare the prototype with the definition of the function on line 25. Note that the name, the return type, and the parameter types are the same. |
|
|
|
|
|
|
|
|
If they are different, a compiler error is generated. In fact, the only required difference is that the function prototype ends with a semicolon and has no body. |
|
|
|
|
|
|
|
|
Also note that the parameter names in the prototype are length and width, but the parameter names in the definition are l and w. It turns out that the names in the prototype are not used; they are there as information to the programmer. The arguments are passed in to the function in the order in which they are declared and defined. |
|
|
|
|
|
|
|
|
The definition of a function consists of the function header and its body. The header is exactly like the function prototype, except that the parameters must be named and there is no terminating semicolon. |
|
|
|
|
|
|
|
|
The body of the function is a set of statements enclosed in braces. Figure 5.3 shows the header and body of a function. |
|
|
|
|
|
|
|
|
A function prototype tells the compiler the function's name, return value, and parameters. |
|
|
|
|
|