Spring 2000, CSE 428: Assignment 3


Distributed: Feb 4.
Due: Feb 10 in class.
Total maximum score: 100 points.

The purpose of this assignment is to provide experience with declarations and parameter-passing methods.
  1. [Points 20] Consider the following program written in a C++like language:
       int x; //global variable
       
       void p(int <parameter-passing-method> y) {
            x = x + 1;
            y = y + 2;
       }
       
       void main() {
            x = 1;
            p(x);
            cout << x;
       }
    
    Say what is the value printed by the program when the parameter-passing-method is:
    1. Call by value
    2. Call by reference
    3. Call by value-result
    Please explain your answers, preferably by using drawings like the ones seen in class (nested boxes illustrating the environment and state at run-time).
  2. [Points 20] Suppose that we want to implement a non-strict (i.e. short-circuit) "logical and" in a language that does not have it, by defining a function
       bool non-strict-and(bool <parameter-passing-method> b1,
                           bool <parameter-passing-method> b2) 
    
    A call of the form
       non-strict-and(e1,e2)
    
    where e1 and e2 are two generic boolean expressions, should give the same result as the C++ expression
       e1 && e2
    
    namely: if the value of e1 is false, then the result should be false; otherwise the result should be the same as the value of e2.

    The characteristic of such a non-strict operator is that if e1 is false then e2 is not evaluated. Hence even if e2 contains a dynamic error (like a division by 0), the evaluation of e1 && e2 does not give an error (if e1 is false).

    1. Explain which of the following parameter-passing-methods can/cannot be used:
      1. Call by value
      2. Call by reference
      3. Call by value-result
      4. Call by name
    2. Define the body of the function.

  3. [Points 20] Implement call by value-result in C++, using call by reference. Namely, given a procedure (in C++like)
       void p(int value-result x){ c } 
    
    where c is a generic sequence of commands, please define the body of an equivalent procedure in C++ using call by reference:
       void p(int &x){ ... } 
    
    You can use, of course, c to refer to the same sequence of command used in the first declaration.
  4. [Points 10] C++ allows declaration of aliases (in some manual they are called "references"); the syntax for declaring a name x of type (say) int, as an alias to an int variable y is:
     
       int &x = y;
    
    Consider the following two fragments of code in C++:
       int &x = y;  // create an alias x to y
       x = x + 1;
       cout << y;
    
    and
       int *x;      // create a variable x of type pointer 
       x = &y;      // assign to x the location address of y
       *x = *x + 1;
       cout << y;
    
    Are these two fragments equivalent, in the sense that they print the same value (independently from the initial value of y)? Please explain, preferably by using drawings.
  5. [Points 30] Consider a language C++like that has call by value but does not have call by reference. We would like to implement call by reference in such a language. Namely, given a C++ procedure
       void p(int &x){ c } 
    
    we would like to define an equivalenty procedure
       void p(...){...} 
    
    using only call by value. For each of the following cases, say whether this is possible and, in the positive case, show how p should be defined.
    1. The language allows declaration of aliases, but not pointers
    2. The langauge allows pointers, but not declaration of aliases.