title: Static (Lexical) Scoping vs Dynamic Scoping (Pseudocode)
body: <pre><code>Program A()
  {
      x, y, z: integer;
  
      procedure B()
      {
          y: integer;
          y=0;
          x=z+1;
          z=y+2;
      }
  
      procedure C()
      {
          z: integer;
  
          procedure D()
          {
              x: integer;
              x = z + 1;
              y = x + 1;
              call B();
          }
  
          z = 5;
          call D();
      }
  
      x = 10;
      y = 11;
      z = 12;
      call C();
      print x, y, z;
  }
  </code></pre>
  
  <p>From my understanding, the result of this program when run using <strong>static scoping</strong> is: x=13, y=7, and z=2.</p>
  
  <p>However, when it is run using <strong>dynamic scoping</strong>, the result is: x=10, y=7, and z=12.</p>
  
  <p>These results are the ones that our professor gave us. However, I cannot understand for the life of me how he has reached these results. Could someone possibly walk through the pseudocode and explain their values in the two different types of scopes?</p>