Assignment 3 for CSCI 431

Overview

In this assignment you will experiment with a couple of the parameter passing mechanisms that we discussed in class using the Pascal programming language. The assignment will be broken into four parts and you will be given a file (containing a Pascal program) to work with in each part.

Due Date: Sept 14 at the beginning of class.

Part 1: Value and var parameters

In Pascal parameters are passed by value by default, while var parameters are passed by reference. Consider the code in the file caution.pas. The procedure p has 2 parameters, one passed by value and one by reference. Within p, c is a local variable, but d is not bound locally, hence refers to the global variable d. Caution must be taken when tracing execution, however, since the actual parameters are in a different order than the formal parameters, though the same names are used. For part 1 of this assignment you are asked to execute the program caution.pas and observe the output, then trace the execution of the program by drawing the associated sequence of activation records.

What you turn in:Your trace of the activation records.

Part 2: Result parameters

Pascal does not support result parameters, but we can attempt to simulate them using the existing parameter types. For example, if we wished to simulate a procedure with result parameters, such as in (in pseudocode):


   procedure p(result x: integer);
      begin
         ...
      end;
For result parameters, the parameter acts as a local storage and the result is copied back to the actual parameter upon exit. Hence the result parameter for procedure p might be simulated in Pascal as:

   procedure p(var y: integer);
      var x: integer;
      begin
         ...
         y := x;
      end;

Using this form, x is effectively undefined upon the start of the procedure, then the resulting x value is copied back via a var parameter to the formal parameter.

Now consider the issue mentioned in the text---the time of address binding for the return value. It can be done either at the time of the call or just before returning from the procedure. Examine the code in the file bindi.pas. in which the above simulation for a result parameter is used. Execute bindi and evaluate the output.

What you turn in: The answer to these questions:

Part 3: Value-result parameters

Value-result parameters can be simulated in a manner similar to that used in Part 2. Consider a pseudocode procedure of the form:


   procedure p(valueresult x: integer);
      begin
         ...
      end;
Since the value of the actual parameter is to be copied to x and the result transferred back, this may be simulated as follows:

   procedure p(var y: integer);
      var x: integer;
      begin
         x := y;
         ...
         y := x;
      end;

Examine the code in program inout.pas in which the parameter x of identical procedures p1 and p2 is passed by reference. Modify procedure p2 so that it passes by value-result (using the simulation method). Save your revised program under the name inout2.pas.

What you turn in: inout2.pas

Part 4: Collisions for value-result parameters

Value-result parameters can result in collisions. Consider the program coll.pas, in which identical procedures p1 and p2 pass two parameters x and y by reference. When converting this to value-result parameters using the above simulation method, however, the two parameters may be copied in different orders. Modify the two procedures p1 and p2 so that they are both pass by (simulated) value-result, but generate different output. Save your revised program under the name coll2.pas. Since such results can be ambiguous for value-result parameters, this is called a collision.

What you turn in: coll2.pas