GATE / CS ARCHIVE

GATE CSE 1999 / QUESTION 15(i)

Trace call by value

Programming in C · Functions & Parameter Passing · Full GATE CSE 1999 paper

GATE 1999, Q15(i) Descriptive
Report issue

Functions & Parameter Passing

Question 15(i), GATE 1999: For this program, determine printed a under call by value:
procedure P(x,y,z);
begin y := y+1; z := x+x end;
begin
  a := 2; b := 3;
  P(a+b,a,a);
  print(a)
end.
The initialization a:=2 is supplied by GFG; GateForum omits it.
Question 15(i), GATE 1999: For this program, determine printed a under call by value:
procedure P(x,y,z);
begin y := y+1; z := x+x end;
begin
  a := 2; b := 3;
  P(a+b,a,a);
  print(a)
end.
The initialization a:=2 is supplied by GFG; GateForum omits it.
Question 15(i), GATE 1999: For this program, determine printed a under call by value:
procedure P(x,y,z);
begin y := y+1; z := x+x end;
begin
  a := 2; b := 3;
  P(a+b,a,a);
  print(a)
end.
The initialization a:=2 is supplied by GFG; GateForum omits it.
Tap to enlarge
Answer & solution

Answer: 2 (no official key was published)

At entry the actual expression a+b is evaluated to 5. Call by value creates three independent local cells x=5, y=2 and z=2. The first assignment changes only local y from 2 to 3. The second computes local z=x+x=10. Neither assignment writes caller a; the local cells disappear when P returns. Hence caller a remains 2 and print(a) outputs 2. The two occurrences of actual a do not create aliases in call by value.

Original paper, page 8