Solutions to Homework 2 ----------------------- 1) Pass by value-result, output: 5 13 200 20 175 190 Pass by name, output: 5 8 200 150 175 190 Pass by reference, output: 5 8 200 20 175 190 Pass by value, output: 10 13 200 150 175 190 2) A is never evaluated, so R(1,1) is never called. output: 0 3) Any variable whose address can only be resolved at run time may cause this procedure to not function correctly. Specifically, when the address of one variable depends on the value of the other. e.g. Swap(i,c[i]). 4) The least restrictive condition: do not allow a variable whose address can only be resolved at run time to be an actual parameter. 5) output: 3 0 15 3 6) The primary problem we have to deal with is that, in effect, we are going to discard the activation record of the procedure making the call. This requires that we have no pointers left to that activation record. Therefore, we can deal with any kind of value parameter passing scheme, or passing a non-local variable or reference parameter by reference. We run into trouble when passing a local variable or value parameter by reference: procedure P(var x : integer) var y: integer; begin if x <> 0 then begin y := x+10; y := x-1; writeln(y); P(y); end; end; In both of these cases, a straightforward implementation of tail-recursion would result in x having the address of the current instance of y, whichw would produce semantics different from what would be expected. Note that when passing value parameters by value, we must be careful to perform the exchanges in the right order (possibly using temporary variables) so that we do not clobber values. Consider the implementation of tail recursion for the following procedure. procedure P(x,y : integer) begin if x <> 0 then begin x := x-1; writeln(y); P(y,x); end; end; In making the recursive call, we must be sure to swap the values of x and y.