< previous page page_347 next page >

Page 347
        cout << tennis. << endl;
    else if (temp > 32)
        cout << golf. << endl;
    else if (temp > 0)
        cout << skiing. << endl;
    else
        cout << dancing. << endl;
}
In the Activity program, the actual parameters in the two function calls are both named temperature. The formal parameter in GetTemp is a reference parameter named temp. The formal parameter in PrintActivity is a value parameter, also named temp.
The main function tells GetTemp where to leave the temperature by giving it the location of the variable temperature when it makes the function call. We must use a reference parameter here so that GetTemp knows where to deposit the result. In a sense, the formal parameter temp is just a convenient placeholder in the function definition. When GetTemp is called with temperature as its actual parameter, all the references to temp inside the function actually are made to temperature. If the function were to be called again with a different variable as an actual parameter, all the references to temp would actually refer to that other variable until the function returned control to main.
In contrast, PrintActivity's formal parameter is a value parameter. When PrintActivity is called, main sends a copy of the value of temperature for the function to work with. It's appropriate to use a value parameter in this case because PrintActivity is not supposed to modify the actual parameter temperature.
Because actual and formal parameters can have different names, we can call a function with different actual parameters. Suppose we wanted to change the Activity program to print an activity for both the indoor and outdoor temperatures. We could declare integer variables in the main function named indoorTemp and outdoorTemp, then write the body of main as:
GetTemp(indoorTemp);
PrintActivity(indoorTemp);
GetTemp(outdoorTemp);
PrintActivity(outdoorTemp);
return 0;
In GetTemp and PrintActivity, the formal parameters would receive values from, or pass values to, either indoorTemp or outdoorTemp.
The following table summarizes the different kinds of parameters that we've seen.

 
< previous page page_347 next page >