|
|
|
|
|
|
|
(quantity), the second is the code (catalog number), and so on. In other words, he looks only at the position of each space on the form. This is how actual parameters are matched to formal parametersby their relative positions in the two parameter lists. |
|
|
|
|
|
|
|
|
Matching Actual Parameters with Formal Parameters |
|
|
|
|
|
|
|
|
Earlier we said that with reference parameters, the actual parameter and formal parameter become synonyms for the same memory location. When a function returns control to its caller, the link between the actual and the formal parameters is broken. They are synonymous only during a particular call to the function. The only evidence that a matchup between the two parameters ever occurred is that the contents of the actual parameter may have changed (see Figure 7-2). |
|
|
|
|
|
|
|
|
Only a variable can be passed as an actual parameter to a reference parameter because a function can assign a new value to the actual parameter. (In contrast, remember that an arbitrarily complicated expression can be passed to a value parameter.) Suppose that we have a function with the following heading: |
|
|
|
|
|
|
|
|
void DoThis( float val, // Value parameter
int& count ) // Reference parameter |
|
|
|
|
|
|
|
|
Then the following function calls are all valid. |
|
|
|
|
|
|
|
|
Figure 7-2
Using a Formal Reference Parameter to Access an Actual Parameter |
|
|
|
|
|