< previous page page_351 next page >

Page 351
Finally, it is up to the programmer to make sure that the formal and actual parameter lists match up semantically as well as syntactically. For example, suppose we had written the indoor/outdoor modification to the Activity program as follows.
int main()
{
    .
    .
    .
    GetTemp(indoorTemp);
    PrintActivity(indoorTemp);
    GetTemp(outdoorTemp);
    PrintActivity(indoorTemp); // Wrong actual parameter
    return 0;
}
The parameter list in the last function call matches the formal parameter list in its number and type of parameters, so no syntax error would be signaled. However, the output would be erroneous because the actual parameter is the wrong temperature value. Similarly, if a function has two formal parameters of the same data type, you must be careful that the actual parameters are in the right order. If they are in the wrong order, no syntax error will result, but the answers will be wrong.
THEORETICAL FOUNDATIONS
Parameter-Passing Mechanisms
There are three major ways of passing parameters to and from subprograms. C++ supports only two of these mechanisms; however, it's useful to know about all three in case you have occasion to use them in another language.
C++ reference parameters employ a mechanism called pass-by-address or pass-by-location. A memory address is passed to the function. Another name for this is pass-by-reference because the function can refer directly to the actual parameter.
C++ value parameters are an example of pass-by-value. The function receives a copy of the value of the actual parameter. Pass-by-value can be less efficient than pass-by-address because the value of a parameter may occupy many memory locations (as we see in Chapter 11), whereas an address usually occupies only a single location. For the simple data types int, char, and float, the efficiency of either mechanism is about the same.
A third method of passing parameters is called pass-by-name. The actual parameter is passed to the function as a character string that must be interpreted by special run-

(text box continues on next page)

 
< previous page page_351 next page >