The idea of an environment is one which is heavily used in functional programming and for interpreters. However the idea can help us understand the difference between global and local scope.

An environment is simply a data structure. It stores variables inside it. Effetely it is a hash table where the key is the variable name and the value is the variables value. When a program we can say that we create a environment from scratch. Initially the environment will be empty. Looking at our code example, line 1 we create a variable. This variable will be added to our environment. As such after line 1 it will look like this. Note – When you create a variable is some languages it is considered not to have a value or the value can not be trusted. Certainly c++ is a language like this.

Variable Name

Value

A

0 (Not defined)

Line 2 assigns a value. From now on all assignments will act upon the environment. As such it will look up in the hash table for the variable name on the left hand side of the assignment. It will then replace the value with the right-hand side of the assignment. An important point to note at this point is that if the variable is not in the hash table when an assignment statement is called, the statement must fail. The table will now look like this.

Variable Name

Value

A

10

Next, on line 3, we call function x(). This has 2 parameters named B and C. B and C will be placed into the environment, however if we do this straight away with no other work we will effectively be creating more global variables. As such each function will create its own environment. So x()’s environment will look like the following.

Variable Name

Value

B

99

C

5

But what happens to our global environment. What happens is that the global environment is passed through as a parameter and is always passed. As such our code now becomes

  1. Dim A
  2. A = 10
  3. x(99,5,env)
  4. Function x(B,C, env)
  5. z(env)
  6. Dim D
  7. D = B + C + A
  8. A = 50
  9. Print A
  10. y(B, env)
  11. Print A
  12. A = D
  13. Print A
  14. End
  15. Function y(B, env)
  16. Dim A
  17. A = B
  18. Print A
  19. End
  20. Function z(env)
  21. print A
  22. end

So every time we create a function we always add the environment as a parameter and every time we call a function we pass the environment through. As such we get the following. Notice that there is a table within a table.

Variable Name

Value

B

99

C

5

env

Text Box: Variable Name  Value    A  10

 

At this stage ignore the obvious inefficiency of this model, it is merely a tool to explain what is happening rather than the reality.

Line 5 now calls function z(). At this point we are going to be passing an environment through. It is important to understand that the environment we pass through is the exact same as the one past through in the env argument, as shown below.

Variable Name

Value

A

10

At line 21 it says Print A. We then check the environment for that function. As we have passed through the global environment we have access to A. As such we can print it out with no problems.

Line 6 now creates a new variable. We are back in the x() function and we use the environment created for that function. Line 7 will then do an assignment. Again the A is looked up from the environment. It will now look like the following.

Variable Name

Value

B

99

C

5

D

114

env

Text Box: Variable Name  Value    A  10

 

Now we have an assignment to a global variable. As the local environment will be destroyed we can now see why we have a table within a table. As A is in the global environment it will update this environment. As we are always passing the global environment around, env, it will then be parentally changed for all other functions. Again this is an important point. As env is always passed, any changes to it will inevitably be passed along as well. This way we have a stable mechanism for global variables.

Variable Name

Value

B

99

C

5

D

114

env

Text Box: Variable Name  Value    A  50