Jade Yu Cheng
ICS 313
Homework #11
March 3, 2009
Homework #11: All the questions in the problem set of Chapter 5
Question 8:Consider the following Ada skeletal program:
procedure main is
X : Integer;
procedure Sub3; This is a declaration of Sub3
It allows Sub1 to call it
procedure Sub1 is
X : Integer;
procedure Sub2 is
begin of Sub2
...
end; of Sub2
begin of Sub1
...
end; Sub1
procedure Sub3 is
begin of Sub3
...
end; of Sub3
begin of Main
...
end; of Main
Assume that the execution of this program is in the following unit order:
Main calls Sub1
Sub1 calls Sub2
Sub2 calls Sub3
Answer:
Sub1 gets its value of X from Sub1
Sub2 gets its value of X from Sub1
Sub3 gets its value of X from Main
b. Repeat part a, but assume dynamic scoping
Answer:
Sub1 gets its value of X from Sub1
Sub2 gets its value of X from Sub1
Sub3 gets its value of X from Sub1
Question 9: Assume the following Ada program was compiled and executed using
static-scoping rules. What value of X is printed in procedure Sub1? Under dynamic-
scoping rules, what value of X is printed in procedure Sub1?
procedure Main is
X : integer;
procedure Sub1 is
begin of Sub1
Put(X);
end; of Sub1
procedure Sub2 is
X : Integer;
begin of Sub2
X := 10;
Sub1
end; of Sub2
begin of Main
X := 5;
Sub2
end; of Main
a. What value of X is printed in procedure Sub1, static scoping used.
Answer:
X=5
b. What value of X is printed in procedure Sub1, dynamic scoping used.
Answer:
X = 10
Question 10: Consider the following program:
procedure Main is
X, Y, Z : Integer;
procedure Sub1 is
A, Y, Z : Integer;
procedure Sub2 is
A, B, Z : Integer;
begin – of Sub2
...
end; of Sub2
begin – of Sub1
...
end; of Sub1
procedure sub3 is
A, X, W : Integer;
begin of Sub3
...
end; of Sub3
begin of Main
...
end; of Main
List all the variables, along with the program units where they are declared, that are
visible in the bodies of Sub1, Sub2, and sub3, assuming static scoping is used.
Answer:
Variables that are visible in Sub1:
A, Y, Z --------- obtained from Sub1
X --------- obtained from Main
Variables that are visible in Sub2:
A, B, Z --------- obtained from Sub2
Y --------- obtained from Sub1
X --------- obtained from Main
Variables that are visible in Sub3:
A, X, W --------- obtained from Sub2
Y, Z --------- obtained from Main
Question 11: Consider the following program:
procedure Main is
X, Y, Z :Integer;
procedure Sub1 is
A, Y, Z : Integer;
begin of Sub1
...
end; of Sub1
procedure Sub2 is
A, X, W : Integer;
procedure Sub3 is
A, B, Z : Integer;
begin of Sub3
...
end; of Sub3
begin of Sub2
...
end; of Sub2
begin of Main
...
end; of Main
List all the variables, along with the program units where they are declared, that are
visible in the bodies of Sub1, Sub2, and Sub3, assuming static scoping is used.
Answer:
Variables that are visible in Sub1:
A, Y, Z --------- obtained from Sub1
X --------- obtained from Main
Variables that are visible in Sub2:
A, X, W --------- obtained from Sub2
Y, Z --------- obtained from Main
Variables that are visible in Sub3:
A, B, Z --------- obtained from Sub3
X, W --------- obtained from Sub2
Y --------- obtained from Main
Question 12: Consider the following program:
void fun(void) {
int a, b, c; /* definition 1 */
...
while (...) {
int b, c, d; /* definition 2 */
... < 1
while (...) {
int c, d, e; /* definition 3 */
... < 2
}
... < 3
}
... < 4
}
For each of the four marked points in this function, list each visible variable, along with
the number of the definition statement that defines it.
Answer:
Point 1:
b, c, d --------- defined in statement “definition 2”
a --------- defined in statement “definition 1”
Point 2:
c, d, e --------- defined in statement “definition 3”
b --------- defined in statement “definition 2”
a --------- defined in statement “definition 1”
Point 3:
b, c, d --------- defined in statement “definition 2”
a --------- defined in statement “definition 1”
Point 4:
a, b, c --------- defined in statement “definition 1”
Question 13: Consider the following program:
void fun1(void); /* prototype */
void fun2(void); /* prototype */
void fun3(void); /* prototype */
void main() {
int a, b, c;
...
}
void fun1(void) {
int b, c, d;
...
}
void fun2(void) {
int c, d, e;
...
}
void fun3(void) {
int d, e, f;
...
}
Given the following calling sequences and assuming that dynamic scoping is used, what
variables are visible during execution of the last function called? Include with each
visible variable the name of the function in which it was defined.
a. Main call fun1; fun1 calls fun2; fun2 calls fun3.
Answer:
Variables that are visible at the last function call:
a ------------- defined in main
b ------------- defined in fun1
c ------------- defined in fun2
d, e, f ------------- defined in fun3
b. Main call fun1; fun1 calls fun3.
Answer:
Variables that are visible at the last function call:
a ------------- defined in main
b, c ------------- defined in fun1
d, e, f ------------- defined in fun3
c. Main calls fun2; fun2 calls fun3; fun3 calls fun1.
Answer:
Variables that are visible at the last function call:
a ------------- defined in main
e, f ------------- defined in fun3
b, c, d ------------- defined in fun1
d. Main calls fun3; fun3 calls fun1.
Answer:
Variables that are visible at the last function call:
a ------------- defined in main
e, f ------------- defined in fun3
b, c, d ------------- defined in fun1
e. Main calls fun1; fun1 calls fun3; fun3 calls fun2.
Answer:
Variables that are visible at the last function call:
a ------------- defined in main
b ------------- defined in fun1
f ------------- defined in fun3
c, d, e ------------- defined in fun2
f. Main calls fun3; fun3 calls fun2; fun2 calls fun1
Answer:
Variables that are visible at the last function call:
a ------------- defined in main
f ------------- defined in fun3
e ------------- defined in fun2
b, c, d ------------- defined in fun1
Question 14: Consider the following program:
procedure Main is
X, Y, Z : Integer;
procedure Sub1 is
A, Y, Z : Integer;
begin of Sub1
...
end; of Sub1
procedure Sub2 is
A, B, Z : Integer;
begin of Sub2
...
end; of Sub2
procedure Sub3 is
A, X, W : Integer;
begin of Sub3
...
end; of Sub3
begin of Main
...
end; of Main
Given the following calling sequences and assuming that dynamic scoping is used, what
variables are visible during execution of the last subprogram activated? Include with
each visible variable the name of the unit where it is declared
a. Main calls Sub1; Sub1 calls Sub2; Sub2 calls Sub3
Answer:
Variables that are visible at the last function call:
Y ------------- declared in Sub1
B, Z ------------- declared in Sub2
A, X, W ------------- declared in Sub3
b. Main calls Sub1; Sub1 calls Sub3
Answer:
Variables that are visible at the last function call:
Y, Z ------------- declared in Sub1
A, X, W ------------- declared in Sub3
c. Main calls Sub2; Sub2 calls Sub3; Sub3 calls Sub1
Answer:
Variables that are visible at the last function call:
B ------------- declared in Sub2
X, W ------------- declared in Sub3
A, Y, Z ------------- declared in Sub1
d. Main calls Sub3; Sub3 calls Sub1
Answer:
Variables that are visible at the last function call:
X, W ------------- declared in Sub3
A, Y, Z ------------- declared in Sub1
e. Main calls Sub1; Sub1 calls Sub3; sub3 calls Sub2
Answer:
Variables that are visible at the last function call:
Y ------------- declared in Sub1
X, W ------------- declared in Sub3
A, B, Z ------------- declared in Sub2
f. Main calls Sub3; Sub3 calls Sub2; Sub2 calls Sub1
Answer:
Variables that are visible at the last function call:
X, W ------------- declared in Sub3
B ------------- declared in Sub2
A, Y, Z ------------- declared in Sub1