Search code examples
programming-languagesglobal-variablesscopepseudocode

Static and Dynamic Scoping Problem


after reading few post in this forum on this topic i had better understanding on static and dynamic scoping. However, i have encountered a problem from internet and seems to face issues to get around with that. The code is as follows:

x : integer                   –– global
procedure set x(n : integer)
   x := n
procedure print x
   write integer(x)
procedure first
   set x(1)
   print x
procedure second
   x : integer
   set x(2)
   print x

set x(0)
first()
print x
second()
print x

What does this program will print if we use static scoping and What does it print if we use dynamic scoping?


Solution

  • It's not that easy to predict what will happen in some imaginary language after launching a pseudocode but expected result will be:

    1 - procedure 'first' output
    2 - procedure 'second' output
    1 - 'main' output
    

    P.S. those are not called static/dynamic. Those are global/local. At least in those languages I know.