GATE / CS ARCHIVE

SIMILAR QUESTIONS / PROGRAMMING IN C

Trace recursion with a global and value/reference parameters

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

How these are solved

One shared Pascal recur program updates x before recursion and global z on return. A frame/alias trace solves all three variants, but value passing versus reference passing changes storage sharing and one question asks invocation count rather than final z.

Report issue

Trace value-parameter recursion

Recursion

Question 10(a)(i), GATE 1997: Consider this pseudo-Pascal program, where z is global:
program what;
var z:integer;
procedure recur(x);
begin
  if x<=40 then begin
    x:=x+z;
    recur(x);
    z:=x+10
  end
end;
begin
  z:=10;
  recur(z);
  writeln(z)
end.
If x is passed by value, what is printed?
Question 10(a)(i), GATE 1997: Consider this pseudo-Pascal program, where z is global:
program what;
var z:integer;
procedure recur(x);
begin
  if x<=40 then begin
    x:=x+z;
    recur(x);
    z:=x+10
  end
end;
begin
  z:=10;
  recur(z);
  writeln(z)
end.
If x is passed by value, what is printed?
Question 10(a)(i), GATE 1997: Consider this pseudo-Pascal program, where z is global:
program what;
var z:integer;
procedure recur(x);
begin
  if x<=40 then begin
    x:=x+z;
    recur(x);
    z:=x+10
  end
end;
begin
  z:=10;
  recur(z);
  writeln(z)
end.
If x is passed by value, what is printed?
Tap to enlarge
Answer & solution

Answer: 30. (no official key was published)

Step through it (13 steps)

Pass by value gives every call its own local x, but all calls still share the same global z. It is important not to copy z into each activation or to overwrite a caller's x with its child's final x.

On descent, no assignment z:=x+10 has executed yet: it comes after the recursive call. Therefore z stays 10 while the successive calls enter with x=10,20,30,40,50. The calls entering with 10,20,30,40 first update their local x to 20,30,40,50 respectively. The final call enters with x=50; because 50≤40 is false, it immediately returns.

Now unwind the suspended calls:
Entered x=40, saved updated x=50: assign z=60.
Entered x=30, saved updated x=40: assign z=50.
Entered x=20, saved updated x=30: assign z=40.
Entered x=10, saved updated x=20: assign z=30.

The outer call is last to write the shared global, so writeln(z) prints 30. The temporary value 60 produced by the deepest active body is not the final answer. Also, x=40 does execute its body, because both visual sources show ≤40 rather than <40. The individual marks for this nested leaf are not printed; they are left unknown rather than assigning an invented share of the original group's marks.

Original paper, page 7

Report issue

Count value-parameter procedure calls

Functions & Parameter Passing

Question 10(a)(ii), GATE 1997: Consider this pseudo-Pascal program, where z is global:
program what;
var z:integer;
procedure recur(x);
begin
  if x<=40 then begin
    x:=x+z;
    recur(x);
    z:=x+10
  end
end;
begin
  z:=10;
  recur(z);
  writeln(z)
end.
If x is passed by value, how many times is recur called, including the call from the main program?
Question 10(a)(ii), GATE 1997: Consider this pseudo-Pascal program, where z is global:
program what;
var z:integer;
procedure recur(x);
begin
  if x<=40 then begin
    x:=x+z;
    recur(x);
    z:=x+10
  end
end;
begin
  z:=10;
  recur(z);
  writeln(z)
end.
If x is passed by value, how many times is recur called, including the call from the main program?
Question 10(a)(ii), GATE 1997: Consider this pseudo-Pascal program, where z is global:
program what;
var z:integer;
procedure recur(x);
begin
  if x<=40 then begin
    x:=x+z;
    recur(x);
    z:=x+10
  end
end;
begin
  z:=10;
  recur(z);
  writeln(z)
end.
If x is passed by value, how many times is recur called, including the call from the main program?
Tap to enlarge
Answer & solution

Answer: 5 total calls, of which 4 are recursive calls. (no official key was published)

Step through it (13 steps)

At every recursive descent, the global z is still 10, since z is assigned only after recur(x) returns. Each active call therefore adds 10 to its own copy of x and invokes the next call. The entry arguments are

Main → recur(10) → recur(20) → recur(30) → recur(40) → recur(50).

The first four entries satisfy x≤40 and execute the recursive-call statement. The last entry, with x=50, fails the test and returns without another call. It is nevertheless an invocation and must be included when counting how many times recur is called. Thus there are five calls in total, four of them made recursively by the procedure itself.

A count of four confuses the number of executed recursive-call statements, or the number of nonbase activations, with the total requested calls. A count based on doubling x would instead be using reference semantics, in which x aliases z; that is not this subpart. The literal ≤ is also essential: a call with argument 40 recurses once more. Original10(a)(ii) is a separate normalized leaf, but neither source prints its own mark allocation, so its marks remain null.

Original paper, page 7

2 marks · Descriptive
Report issue

Trace reference-parameter recursion

Functions & Parameter Passing

Question 10(b), GATE 1997: Consider this pseudo-Pascal program, where z is global:
program what;
var z:integer;
procedure recur(x);
begin
  if x<=40 then begin
    x:=x+z;
    recur(x);
    z:=x+10
  end
end;
begin
  z:=10;
  recur(z);
  writeln(z)
end.
If x is passed by reference, what is printed?
Question 10(b), GATE 1997: Consider this pseudo-Pascal program, where z is global:
program what;
var z:integer;
procedure recur(x);
begin
  if x<=40 then begin
    x:=x+z;
    recur(x);
    z:=x+10
  end
end;
begin
  z:=10;
  recur(z);
  writeln(z)
end.
If x is passed by reference, what is printed?
Tap to enlarge
Answer & solution

Answer: 110. (no official key was published)

Step through it (11 steps)

The main program calls recur(z), so its reference parameter x is another name for the global variable z. Passing that reference onward as recur(x) keeps the same alias in every activation. There are not separate saved integer values of x in the suspended calls.

Initially x=z=10. The first body executes x:=x+z, doubling the shared variable to 20. The next call doubles it to 40. Because the guard is x≤40, the call seeing 40 also executes the body and doubles it to 80. The next call sees 80 and returns immediately. Thus four activations exist, three with a pending final assignment.

On unwinding, every z:=x+10 reads the current shared value, because x aliases z. The three pending assignments make the values 80→90→100→110. The program consequently prints 110.

It would be incorrect to use the descent values 10,20,40 as saved local copies during unwinding: that is pass-by-value behavior. It would also be incorrect to stop at 80, since the three assignments following recursive calls still have to execute. The example shows why mutation of a reference argument can interact unexpectedly with a global variable naming the same storage location.

Original paper, page 7