You are on page 1of 9

Consider the following program fragment written in a

pseudo-language that uses both static scope and dynamic


scope.
1.
var y = "global";

function print-y() {
print(y);
}

function test-scope() {
var y = "local";
print-y();
}
test-scope();
print-y();
2.

3.
4.
int N;
void main()
{ N=1;
A();
printf(“main: %1d”,N);
}

void A()
int N;
{ N = 2;
B();
printf(“A: %1d . ”, N);
}

void B()
{ N = 3; }
5. Program A()
{
x, y, z: integer;

procedure B()
{
y: integer;
y=0;
x=z+1;
z=y+2;
}
procedure C()
{
z: integer;

procedure D()
{
x: integer;
x = z + 1;
y = x + 1;
call B();
}

z = 5;
call D();
}

x = 10;
y = 11;
z = 12;
call C();
print x, y, z;
}

6.
program a() {
x: integer; // "x1" in discussions below
x = 1;

procedure b() {
x = 2; // <-- which "x" do we write to?
}

procedure c() {
x: integer; // "x2" in discussions below
b();
}
c();
print x;
}

7. int a;
Procedure foo
a=10;
goo();
hoo();
write (a);
Procedure goo
a=20
Procedure hoo
write (a)
Procedure main
int a;
a=30;
foo();
write (a)

8. int x=3;
void f(int x)
{ g();
}
void g()
{
Print (x);
}
void main()
{
int x=12;
f(42);
g();
}

9. int a
void first()
{a=1;}
void second()
{ int a=3;
first();
}
void main()
{
a=2;
second();
printf(“%d,a”)
}

10. begin
integer m, n;
procedure hardy;
begin
print("in hardy -- n = ", n);
end;

procedure laurel(n: integer);


begin
print("in laurel -- m = ", m);
print("in laurel -- n = ", n);
hardy;
end;

m := 50;
n := 100;
print("in main program -- n = ", n);
laurel(1);
hardy;
end;

You might also like