You may of noticed already the code for function y().

  1. Function y(B, env)
  2. Dim A
  3. A = B
  4. Print A
  5. End

We will be looking at this code exclusively for this section and as such line numbers will start from 1.

Looking at line 2 we can see that we are trying to create a new variable called A. But how can we do this as a variable already exists in the global environment. We could cause the program to fail here. However we do not. We allow the variable to be reused within the function. As such our environment will look like this after line 3.

Variable Name

Value

B

99

A

99

env

Text Box: Variable Name  Value    A  50

 

We have two values for A now, which one should we use. The answer is that we always use the variable which is in the lowest table. As we have nested tables we always use the one in the lowest nesting. In this case we will treat A to be 99 rather than 50.

This is known as variable shadowing and is a very powerful feature of most languages. It is especially useful if you wish to reuse variable names to aid the understanding of your code. It is even more important when it comes to object orientated programming.