Exercise 13      Name                                    Points __/22

Document last modified: 

 

  1. (10) Scoping
  2. Give the static scoping diagram.

  3. What is the result with static scoping?

  4. Give the dynamic scoping diagram.

  5. What is the result with dynamic scope?

int z = 2;

int add ( int f(int), int z) {  return f (z); }

int p(int x) { return x+z; }

void main() { cout << add( p, 3); }

2. (12 pts) What is the output of the following for:

  1. x is passed by value and y is passed by value;
  2. x is passed by value and y is passed by name;
  3. x is passed by name and y is passed by value; and
  4. x is passed by name and y is passed by name?
#include <iostream.h>

int I = 0;
int A[3] = { 7, 11, 13 };

void P(int x, int y) {
        y = 1;
        cout << x << "\t";
        I = 2;
        cout << x << "\t";
        I = 2;
        cout << x << "\t";
        cout << y << "\n";
}

void main() {
        P( A[I], I );
        P( I   , A[I] );
 }