Runtime Environments & Parameter Passing
![Question 10(a), GATE 1992: For P(A[i]), where P has an integer formal parameter, a proposed implementation creates local z, sets z:=A[i], runs P’s body using z, then writes A[i]:=z. Identify the parameter-passing mechanism, judge correctness and give a small correction.](../../assets/questions/CS-1992-Q53-1.webp)
Answer & solution
Answer: Call by value-result (copy-in/copy-out); capture the actual argument’s location at call time and copy back to that same location. (no official key was published)
The separate local variable receives the initial value and is copied back on return, so the intended mechanism is call by value-result, also called copy-in/copy-out. It is not call by reference, which uses the actual storage throughout execution, nor call by value, which never copies a final value back. Call by name re-evaluates an actual-argument expression in its caller environment on each use rather than using one private copied value.
The proposed copy-back re-evaluates A[i]. If the caller’s index changes during execution, it can select a different element from the one used at entry. For example initially i=2 and A[2]=10. Suppose the body changes the caller-visible index to 3 through a global reference and leaves the formal’s local value as 99. Re-evaluating A[i] on exit writes A[3]=99 instead of copying back into A[2]. An implementation must preserve the original actual location under the usual location-fixed value-result convention.
Small correction
p:=address_of(A[i]); {evaluate actual location in caller environment once}
z:=value_at(p); {copy in}
execute P body with every occurrence of its formal bound to z;
value_at(p):=z; {copy out to saved location}
If A is known not to change identity or base address, saving k:=i and using A[k] for both transfers is sufficient; saving the address is the general correction. Normal return is assumed, and with one parameter there is no multiple-copy-back ordering issue.There is also a naming issue in the printed wording: P’s formal is named i, while the actual A[i] contains the caller’s i. They are different bindings. The body must substitute z for the formal, not literally search for the caller expression A[i] in the body. Merely assigning the formal does not itself change the caller’s separate index; the failure requires that the caller-visible index or array location can otherwise change. If that cannot happen, the original sequence happens to produce the right result.