GATE / CS ARCHIVE

GATE CSE 1999 / QUESTION 15(ii)

Trace reference passing with an expression

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

GATE 1999, Q15(ii) Descriptive
Report issue

Functions & Parameter Passing

Question 15(ii), GATE 1999: Determine printed a under call by reference for:
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.
Use GFG’s visible a:=2 initialization, absent in GateForum, and discuss passing the expression a+b by reference.
Question 15(ii), GATE 1999: Determine printed a under call by reference for:
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.
Use GFG’s visible a:=2 initialization, absent in GateForum, and discuss passing the expression a+b by reference.
Question 15(ii), GATE 1999: Determine printed a under call by reference for:
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.
Use GFG’s visible a:=2 initialization, absent in GateForum, and discuss passing the expression a+b by reference.
Tap to enlarge
Answer & solution

Answer: 10 under expression-temporary convention; strict variable-reference semantics reject the call. (no official key was published)

There is a language-semantic qualification: an ordinary assignable reference parameter cannot bind to the non-variable expression a+b. In a strict Pascal-style var-parameter language this call is invalid, so no output is defined. The customary exercise convention evaluates a+b once into an anonymous temporary t=5 and binds x to t, while both y and z refer directly to caller a. Then y:=y+1 changes a from 2 to 3. Next z:=x+x reads t twice, obtains 10, and writes a=10. Thus the intended trace under that convention prints 10. It does not print 12: reevaluating a+b after a changes would be call-by-name behavior, not a reference to the original temporary. The convention and the secondary initialization must both be stated rather than silently assumed.

Original paper, page 8