GATE / CS ARCHIVE

GATE CSE 1991 / QUESTION 9(a.i)

Call by value with an expression argument

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

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

Runtime Environments & Parameter Passing

Question 9(a.i), 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 value.
Question 9(a.i), 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 value.
Tap to enlarge
Answer & solution

Answer: x=1, z=100. (no official key was published)

With call by value, evaluate the actual arguments in the caller before entering P: x yields 1, x*y yields 5, and z yields 100. Allocate independent local formals with values (a,b,c)=(1,5,100).

The assignment a:=2 changes only the local a. Next c:=a+b computes 2+5=7 and places it in the local c. Neither assignment targets the caller’s variables, so after return x remains 1, y remains 5, and z remains 100. The main program prints x=1,z=100.

The intermediate c=7 is not the value printed by the program: the print is outside P and names z. Ordinary call by value has no copy-out stage. Adding copy-out would change the parameter mechanism to value-result, which is a different question. Evaluating the expression x*y once at the call is crucial for distinguishing this case from call by name.

Original paper, page 6