Consider the following code

  1. Function myCode(env)
  2. Dim A
  3. Dim B
  4. A = 5
  5. For B=1 to 10
  6. Dim A
  7. A = B
  8. Print A
  9. Next
  10. Print A
  11. End

Here we have a function which has two definitions of A. So what will the code print out. The answer is 1,2,3,4,5,6,7,8,9,10,5. Notice that the last number it prints is 5 not another 10. Each iteration of the loop will print A. Then after the loop it again prints A. So what is actually going on? The answer is simple. At each block of code we create a new environment. We show blocks of code as indentations to help aid the programmer. Basically loops, control statements and procedures are treated as blocks of code.

Each block of code can define its own local variables and these can shadow previous values. In order to understand this we can label the variables with there scoping level. 0 will be global, 1 will be functions and then so on for each block.

  1. Function myCode(env)
  2. Dim A `1
  3. Dim B `1
  4. A = 5 `1
  1. For B=1 to 10 `1
  2. Dim A `2
  3. A`2 = B`1
  4. Print A `2
  5. Next
  6. Print A `1
  7. End

The code has had numbers added to it. The numbers show the block level. The for loop is block 2 while the rest of the function is block 1.