GATE / CS ARCHIVE

SIMILAR QUESTIONS / COMPILER DESIGN

Trace local copies of value parameters

2 questions solved the same way, newest first. Back to Compiler Design

How these are solved

Copy actual values into formals and trace local mutation without changing caller storage. Different programs; mixed value/reference example excluded.

2 marks · MCQ
Report issue

A value parameter shadows a global

Runtime Environments & Parameter Passing

Question 2.4, GATE 1995: Pascal has global integer X=2 and procedure FIND(X:real) assigning X:=sqrt(X). After FIND(X), what does printing global X produce?
Tap to enlarge
Your answer
Answer & solution

Answer: A: 2 under the intended Pascal value-parameter program (no official key was published)

Without the keyword var, the procedure parameter is passed by value. Its local X receives a real copy of the argument and shadows the global name inside FIND. Assigning sqrt(X) changes only that local copy; it does not assign to the global integer. On return the global X is still 2, so printing it yields 2. The the paper omits some statement separators; this analysis uses the intended syntactically separated program rather than treating formatting omissions as executable Pascal.

Original paper, page 3

Descriptive
Report issue

Call by value with an expression argument

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