6.7k views
0 votes
Consider the following skeletal C-like 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 calls funl; funl calls fun2; fun2 calls fun3
b. main calls fun1; fun1 calls fun3
c. main calls fun2; fun2 calls fun3; fun3 calls funl
d. main calls fun1; funl calls fun3; fun3 calls fun2.

User Guerra
by
4.8k points

1 Answer

2 votes

Answer:

In dynamic scoping the current block is searched by the compiler and then all calling functions consecutively e.g. if a function a() calls a separately defined function b() then b() does have access to the local variables of a(). The visible variables with the name of the function in which it was defined are given below.

Step-by-step explanation:

a. main calls fun1; fun1 calls fun2; fun2 calls fun3

Solution:

  • Visible Variable: d, e, f Defined in: fun3
  • Visible Variable: c Defined in: fun2 ( the variables d and e of fun2 are not visible)
  • Visible Variable: b Defined in: fun1 ( c and d of func1 are hidden)
  • Visible Variable: a Defined in: main (b,c are hidden)

b. main calls fun1; fun1 calls fun3

Solution:

  • Visible Variable: d, e, f Defined in: fun3
  • Visible Variable: b, c Defined in: fun1 (d not visible)
  • Visible Variable: a Defined in: main ( b and c not visible)

c. main calls fun2; fun2 calls fun3; fun3 calls fun1

Solution:

  • Visible Variable: b, c, d Defined in: fun1
  • Visible Variable: e, f Defined in: fun3 ( d not visible)
  • Visible Variable: a Defined in: main ( b and c not visible)

Here variables c, d and e of fun2 are not visible .

d. main calls fun1; fun1 calls fun3; fun3 calls fun2

Solution:

  • Visible Variable: c, d, e Defined in: fun2
  • Visible Variable: f Defined in: fun3 ( d and e not visible)
  • Visible Variable: b Defined in: fun1 ( c and d not visible)
  • Visible Variable: a Defined in: main ( b and c not visible)
User Artiga
by
5.5k points