UNIT 1 - ⇑ Fundamentals of Programming ⇑ | ||
← Functions and Procedures | Global and Local Variables | The Role of Variables → |
Global variable - declared at the start of the program, their global scope means they can be used in any procedure or subroutine in the program
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 variable - declared within subroutines or programming blocks, their local scope means they can only be used within the subroutine or program block they were declared in
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:
Module GlocalsDim number1 as integer = 123Sub Main()console.writeline(number1)printLocalNumber()printGlobalNumber()End SubSub printLocalNumberDim number1 as integer = 234console.writeline(number1)End SubSub printGlobalNumberconsole.writeline(number1)End SubEnd Module
What would the output be?
Code Output
123
234
123
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 subroutineprintLocalNumber()
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 subroutineprintGlobalNumber()
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
Title
What is the difference between a global and a local variable?
Answer:
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
Why is it a good idea to use local variables instead of global variable?
Answer:
Local variables release memory when you have finished with them, global variables are always stored in memory whether you need them or not
In what situation might you want to use a global variable?
Answer:
When you want to declare a variable that needs to be accessible by all parts of your code
List the global and local variables for the following. What will be the output for input 16
:
Module calcAgeSub Main()dim age as integerconsole.writeline("age?")age = console.readline()printMonths(age)printDays(age)End SubDim months as integerSub printDays(a)Dim d as integerd = a * 365console.writeline(d)End SubSub printMonths(a)months = a * 12console.writeline(months)End SubEnd Module
Answer:
Locals: age, d
Globals: months
Code Output
age?
16
192
5840
For the above code how could you make the code more efficient, and why would it be more efficient?
Answer:
Make the months
variable a local variable by putting it inside the printMonths(a)
sub routine, if you leave it as a global variable it will be taking up memory even when you don't need it.
List the Global and Local variables in the following code, list the output:
Module greetingsDim q as integer = 6Sub sayGoodbye()for y = 1 to qconsole.write("bye,")loopEnd SubSub sayHello()dim q as integer = 4if q =< 4 thenconsole.write("hi,")elseconsole.write("hello,")endifEnd SubSub Main()console.writeline(q)sayHello()sayGoodbye()console.writeline(q)End SubEnd Module
Answer:
Locals: y (on line 4), q (on line 9)
Globals: q (on line 2)
Code Output
6
hi,bye,bye,bye,bye,bye,bye
6
Retrieved from ""