I get confused when dealing with the static scope and dynamic scope, and for this pseudocode I have to find both. For both I believe it should print out: 9 4 2 3 however, I’m not entirely sure. Any help would be appreciated, thanks.
g: integer
procedure B(a: integer)
x: integer
x := a X a
R(1)
procedure A(n: integer)
g := n
procedure R(m: integer)
write_integer(x)
x /:= 2 -- integer division
if x > 1
R(m + 1)
else
A(m)
procedure main() -- entry point
B(3)
write_integer(g)
If your language were statically scoped, then the variable
xwould be local only to the functionB, and it would not be visible outsideB.However, your language is dynamically scoped: The first time the control flow passes over the line
x: integer, the variablexis now globally accessible.So, here’s the order of events:
B(3)assignsx := 9R(1)writes9and assignsx = 4and callsR(2)R(2)writes4and assignsx = 2and callsR(3)R(3)writes2and assignxx = 1and callsA(3)A(3)assignsg = 3write_integer(g)prints3.