Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider the following program: PROCEDURE STOCK IS Y: Float:=8.3; PROCEDURE MU1

ID: 3582254 • Letter: C

Question

Consider the following program:
PROCEDURE STOCK IS
Y: Float:=8.3;
  PROCEDURE MU1 IS
   BEGIN --OF MU1
   Y:=Y-3.1;
   END; --OF MU1
  PROCEDURE MU2 IS
  Y: Float:=9.8;
   BEGIN --OF MU2
   END; --OF MU2
  PROCEDURE MU3
   BEGIN --OF MU3
   END; --OF MU3
BEGIN --OF STOCK
   END; --OF STOCK

Calculate variable Y for two different call sequences:
Dynamic scope
* STOCK calls MU3 which calls MU1
* STOCK calls MU2 which calls MU1
Static scope
*STOCK calls MU3 which class MU1
*STOCK calls MU2 which calls MU1

Explanation / Answer

Static scoping means that x refers to the x declared in innermost scope of declaration that has one. Dynamic scoping means that x refers to the x declared in the most recent frame of the call-stack the has one

MU1 is declared under scope of STOCK. Hence in static scoping, MU1 will refer the value Y: Float:=8.3; regardless of from which function it is called.

Static scope
*STOCK calls MU3 which class MU1
*STOCK calls MU2 which calls MU1

In the both cases Y will be Y = 8.3 - 3.1 = 5.2

Dynamic scope
* STOCK calls MU3 which calls MU1

In this case, MU1 will look up the call stack for value of Y. In this case Y = 8.3 will be present in the stack since the function call is Stack -> MU3 -> MU1. Since MU3 doesn't have any definition of Y, last definition of Y will be 8.3

Y will be Y = 8.3 - 3.1 = 5.2

* STOCK calls MU2 which calls MU1

In this case Y = 9.8 will be present in the stack since the function call is STACK -> MU2 -> MU1. Since MU2 defines its own Y, when we go through call stack, Y = 9.8 will be retrieved first.

Y will be Y = 9.8 - 3.1 = 6.7