GATE / CS ARCHIVE

SIMILAR QUESTIONS / PROGRAMMING IN C

Trace reference, value and copy-restore parameter bindings

5 questions solved the same way, newest first. Back to Programming in C

How these are solved

These procedures modify formals that can refer to caller storage, and the requested output depends on value copies, reference aliases or delayed copy-back. Draw a caller/formal storage map, execute assignments, then apply copy-back if required.

Descriptive
Report issue

Trace call by value

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

Descriptive
Report issue

Trace reference passing with an expression

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

Report issue

Expose copy-restore aliasing

Functions & Parameter Passing

Question 15(iii), GATE 1999: Determine printed a under call by copy-restore 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.
GFG supplies a:=2, which is missing in GateForum. No copy-back order is specified.
Question 15(iii), GATE 1999: Determine printed a under call by copy-restore 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.
GFG supplies a:=2, which is missing in GateForum. No copy-back order is specified.
Question 15(iii), GATE 1999: Determine printed a under call by copy-restore 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.
GFG supplies a:=2, which is missing in GateForum. No copy-back order is specified.
Tap to enlarge
Answer & solution

Answer: 10 if y restores before z; 3 if z restores before y; no unique output without a convention. (no official key was published)

Copy-in gives independent local values x=5, y=2, z=2. The body ends with x=5, y=3, z=10. Both y and z have the same copy-back destination, caller a, so restoration order matters. Restoring y then z sets a first to 3 and then to 10, printing 10. Restoring z then y sets a first to 10 and finally to 3, printing 3. Thus the given program has no uniquely determined copy-restore result unless the language specifies the order. A left-to-right formal-parameter restoration convention yields 10. As in reference passing, the non-variable actual a+b requires an expression temporary or a rule permitting input-only expressions; strict copy-in/copy-out parameters requiring assignable actuals reject the call. Under the temporary convention restoring unchanged x does not affect a. This is aliasing at copy-back, not aliasing between the local parameter cells.

Original paper, page 8

Descriptive
Report issue

Reference parameter aliasing

Functions & Parameter Passing

Question 19(i), GATE 1996: Global b is set to 3 and test1(b) is called. Procedure test1(a) sets a=5, prints “point 1:” followed by a,b, calls test2, then prints “point 2:” followed by a,b. Procedure test2 sets the global b=10. After test1 returns, the main program prints “point 3:” followed by b. Assume the displayed lexical scope and determine the output with a passed by reference.
Question 19(i), GATE 1996: Global b is set to 3 and test1(b) is called. Procedure test1(a) sets a=5, prints “point 1:” followed by a,b, calls test2, then prints “point 2:” followed by a,b. Procedure test2 sets the global b=10. After test1 returns, the main program prints “point 3:” followed by b. Assume the displayed lexical scope and determine the output with a passed by reference.
Question 19(i), GATE 1996: Global b is set to 3 and test1(b) is called. Procedure test1(a) sets a=5, prints “point 1:” followed by a,b, calls test2, then prints “point 2:” followed by a,b. Procedure test2 sets the global b=10. After test1 returns, the main program prints “point 3:” followed by b. Assume the displayed lexical scope and determine the output with a passed by reference.
Tap to enlarge
Answer & solution

Answer: point 1: 5 5; point 2: 10 10; point 3: 10. (no official key was published)

A reference parameter is another name for the caller's variable, not a private copy of its value. Here the actual parameter is the global b, so a and b designate the same storage throughout test1. Initially that cell contains 3. Executing a:=5 updates the cell itself, which means both a and b now read as 5. Consequently the first output is point 1: 5 5.

Next test2 executes b:=10. It refers to the global b by the displayed lexical scope; this is the same cell already aliased by a. Therefore both reads at the second output produce 10, giving point 2: 10 10. Returning from test1 requires no copy-back for a reference parameter. The global value remains 10, and the final output is point 3: 10.

The important reasoning is storage identity: a:=5 changes b immediately, and test2's assignment to b changes what a observes immediately. The procedure nesting does not create a second b or hide the global declaration. Spaces and line breaks may depend on Pascal's output formatting; the value sequences, in order, are (5,5), (10,10), and (10).

Original paper, page 9

Descriptive
Report issue

Value-result parameter copy-back

Functions & Parameter Passing

Question 19(ii), GATE 1996: Global b is set to 3 and test1(b) is called. Procedure test1(a) sets a=5, prints “point 1:” followed by a,b, calls test2, then prints “point 2:” followed by a,b. Procedure test2 sets the global b=10. After test1 returns, the main program prints “point 3:” followed by b. Assume the displayed lexical scope and determine the output with a passed by value-result (copy-in/copy-out).
Question 19(ii), GATE 1996: Global b is set to 3 and test1(b) is called. Procedure test1(a) sets a=5, prints “point 1:” followed by a,b, calls test2, then prints “point 2:” followed by a,b. Procedure test2 sets the global b=10. After test1 returns, the main program prints “point 3:” followed by b. Assume the displayed lexical scope and determine the output with a passed by value-result (copy-in/copy-out).
Question 19(ii), GATE 1996: Global b is set to 3 and test1(b) is called. Procedure test1(a) sets a=5, prints “point 1:” followed by a,b, calls test2, then prints “point 2:” followed by a,b. Procedure test2 sets the global b=10. After test1 returns, the main program prints “point 3:” followed by b. Assume the displayed lexical scope and determine the output with a passed by value-result (copy-in/copy-out).
Tap to enlarge
Answer & solution

Answer: point 1: 5 3; point 2: 5 10; point 3: 5. (no official key was published)

Value-result creates a separate local parameter cell. At entry to test1, the current actual value b=3 is copied into a. The assignment a:=5 changes only that private cell; the global b remains 3. Thus the first output is point 1: 5 3.

The call to test2 then assigns 10 to the global b. It does not refer to the private parameter a of its caller, so a stays 5. The second output is consequently point 2: 5 10. This is the observable difference from call-by-reference: a is not an alias for b during the call.

At normal return from test1, the result phase copies the final local a back to the original actual parameter location b. That overwrites test2's value 10 with 5. The main program therefore prints point 3: 5. The chronological storage states are (a,b)=(3,3) at entry, (5,3) after assigning a, (5,10) after test2, and b=5 after copy-out. No conflict among multiple value-result actuals arises because the procedure has just one parameter. If it were ordinary call-by-value instead, there would be no final copy-back and point 3 would show 10; the word “result” is essential.

Original paper, page 9