When programming you will already of looked at local and global variables. You will of also most likely used them in your own programs. However at this stage you may not actually understand what is actually going on. In order to understand fully what scope actually means for variables we need to get a little bit formal about the way we treat variables. Consider the following pseudo code

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

 

To help explain this code the lines are numbered. We shall reference the code by using the line numbers. The code itself does nothing useful; however it does show the idea of scoping.

The important thing to note is the Dim keyword. This means that we are creating a variable space. This is important as it helps introduce the idea of shadowing which we will come onto later.

One last thing to note is that function parameters or arguments are always defined as variables. As such there is no need to define them separately.