Fundamentals of Programming: Global and Local Variables
It is seldom advisable to use Global variables as they are liable to cause bugs, waste memory and can be hard to follow when tracing code. If you declare a global variable it will continue to use memory whilst a program is running even if you no longer need/use it.
Local variables are initiated within a limited scope, this means they are declared when a function or subroutine is called, and once the function ends, the memory taken up by the variable is released. This contrasts with global variables which do not release memory.
Take a look at this example:
1 Module Glocals 2 Dim number1 as integer = 123 3 4 Sub Main() 5 console.writeline(number1) 6 printLocalNumber() 7 printGlobalNumber() 8 End Sub 9 10 Sub printLocalNumber 11 Dim number1 as integer = 234 12 console.writeline(number1) 13 End Sub 14 15 Sub printGlobalNumber 16 console.writeline(number1) 17 End Sub 18 End Module
What would the output be?
Why is this? Well we seem to have two versions of the variable number1.
- The first version is declared on line 2, this isn't declared inside any sub routines so the variable has Global scope
- The second version is declared inside the printLocalNumber sub routine. As it is declared inside a sub routine it is only able to be used inside this subroutine. And on line 12 when we use:
console.writeline(number1)it prints out the local variable
So looking at the code inside the main sub routine we have 3 different ways of printing out the variable number1.
- Line 5.
console.writeline(number1):This uses the global value of number1, as it is inside a sub routine with no other local declarations
- Line 6.
printLocalNumber():This is calling on the subroutine
printLocalNumber()which has a local variable number1 contained within it on line 11, therefore it uses the number1 value declared on line 11.
- Line 7.
printGlobalNumber():This is calling on the subroutine
printGlobalNumber()which has no local variable for number1, therefore it uses the global value for number1
Rules of thumb: If you want to quickly tell the difference between a global and a local variable use these quick rules. But be warned they might try to trick you!
- If a variable is declared inside a function or a procedure it is a local variable
- If a variable is declared inside a iterative or selective statement it is local
- If the declaration is indented from the left hand boundary it probably meets one of the above criteria and is local
- If it meets none of the above statements and is declared in the main body of code it is a global variable
Global variables are accessible from all parts of a program, whilst local variables are only accessible within a programming construct such as a loop, function or procedure
Local variables release memory when you have finished with them, global variables are always stored in memory whether you need them or not
When you want to declare a variable that needs to be accessible by all parts of your code
Locals: age, d
Locals: y (on line 4), q (on line 9)
Globals: q (on line 2)