A-level Computing/AQA/Paper 1/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:

What would the output be?

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:   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.
 * 1) Line 5.  :This uses the global value of number1, as it is inside a sub routine with no other local declarations
 * 2) Line 6.  :This is calling on the subroutine   which has a local variable number1 contained within it on line 11, therefore it uses the number1 value declared on line 11.
 * 3) Line 7.  :This is calling on the subroutine   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 an 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

What is the difference between a global and a local 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 Why is it a good idea to use local variables instead of global variable? 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? 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 :

Locals: age, d

Globals: months

age? 16 192 5840

For the above code how could you make the code more efficient, and why would it be more efficient? Make the  variable a local variable by putting it inside the   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:

Locals: y (on line 4), q (on line 9)

Globals: q (on line 2)

6 hi,bye,bye,bye,bye,bye,bye 6