GATE / CS ARCHIVE

GATE CSE 1991 / QUESTION 9(a.ii)

Reference parameters and expression actuals

Compiler Design · Runtime Environments & Parameter Passing · Full GATE CSE 1991 paper

GATE 1991, Q9(a.ii) Descriptive
Report issue

Runtime Environments & Parameter Passing

Question 9(a.ii), GATE 1991: All variables are integers. Procedure P(a,b,c) executes a:=2; c:=a+b. Main executes x:=1; y:=5; z:=100; P(x,x*y,z); write(x,z). Determine the output for call by reference, stating any validity assumption.
Question 9(a.ii), GATE 1991: All variables are integers. Procedure P(a,b,c) executes a:=2; c:=a+b. Main executes x:=1; y:=5; z:=100; P(x,x*y,z); write(x,z). Determine the output for call by reference, stating any validity assumption.
Tap to enlarge
Answer & solution

Answer: Strict reference-only actuals: invalid call, no defined output. If expression actuals are materialized in a temporary: x=2,z=7. (no official key was published)

Pure call by reference binds each formal to an assignable storage location. The actuals x and z have such locations, but x*y is an arithmetic expression and has no variable location to which a conventional reference formal can bind. Thus in a language requiring variable actuals for reference parameters, this call is rejected and there is no output. This is an important qualification to the otherwise familiar textbook calculation.

If the intended pseudo-language permits an expression actual by evaluating it into a temporary, evaluate x*y=1·5=5 at call entry and bind b to that temporary. Bind a directly to x and c directly to z. Executing a:=2 sets x to 2. The temporary b remains 5; it is not a live expression. Then c:=a+b sets z=2+5=7. On return the program prints x=2,z=7.

The temporary convention is sufficient here because P never assigns to b. It is nevertheless an additional semantic rule, not inherent in reference passing. In particular, re-evaluating x*y after changing x would give 10 and would implement call by name rather than this temporary-backed reference convention.

Original paper, page 6