|
|
|
|
|
|
|
When a function is invoked using a reference parameter, it is the location (memory address) of the actual parameter, not its value, that is passed to the function. There is only one copy of the information, and it is used by both the caller and the called function. When a function is called, the actual parameter and formal parameter become synonyms for the same location in memory. Whatever value is left by the called function in this location is the value that the caller will find there. Therefore, you must be careful using a formal reference parameter because any change made to it affects the actual parameter in the calling code. Let's look at an example. |
|
|
|
|
|
|
|
|
The Activity program in Chapter 5 reads in a temperature and prints out an appropriate activity. Here is its design. |
|
|
|
|
|
|
|
|
Get temperature
Print activity |
|
|
|
|
|
|
|
|
|
|
Prompt for temperature value input
Read temperature
Echo-print temperature |
|
|
|
|
|
|
|
|
|
|
|
Print The recommended activity is
IF temperature > 85
Print swimming.
ELSE IF temperature > 70
Print tennis.
ELSE IF temperature > 32
Print golf.
ELSE IF temperature > 0
Print skiing.
ELSE
Print dancing. |
|
|
|
|
|
|
|