Local and Global Variables
Master Local and Global Variables for GCSE Computer Science with this free worksheet and full mark scheme — Foundation and Higher exam-style questions with worked answers for AQA GCSE Computer Science (8525). A local variable exists only inside its subroutine, while a global variable can be used anywhere.
Free downloads
These worksheets and mark schemes are original, written for Virtus Academy and checked against the current AQA specification. Every worksheet comes with a full mark scheme.
Topic overview
The scope of a variable determines where in a program it can be accessed.
A local variable is declared inside a subroutine and can only be used within it. It is created when the subroutine is called and destroyed when it finishes. A global variable is declared outside all subroutines and can be accessed from anywhere in the program.
Local variables are strongly preferred. Because a local variable cannot be changed from outside its subroutine, it prevents one part of a program accidentally affecting another. Global variables can be changed anywhere, which makes bugs hard to trace and makes subroutines dependent on each other rather than self-contained.
Revision notes
Local variables
Declared inside a subroutine and accessible only within it.
Created when the subroutine is called and destroyed when it ends. Two subroutines can each have a variable called count without interfering, because each is separate.
Global variables
Declared outside all subroutines and accessible from anywhere in the program.
They exist for the whole time the program runs. Useful for values genuinely needed everywhere, but they should be used sparingly.
Why local is preferred
A local variable cannot be changed from outside its subroutine.
This prevents one part of the program accidentally affecting another, keeps subroutines self-contained and reusable, and makes bugs easier to trace because the possible causes are limited to one subroutine.
Key points
- Scope determines where a variable can be accessed.
- A local variable works only inside its subroutine.
- It is destroyed when the subroutine ends.
- A global variable is accessible anywhere.
- Local variables prevent accidental interference.
- Global variables make bugs harder to trace.
Worked examples
Example 1
State where a local variable can be accessed. [1 mark]
Working
Example 2
Explain two reasons why local variables are preferred to global variables. [2 marks]
Working
Example 3
Explain why two subroutines can each have a variable called count without conflict. [2 marks]
Working
Common mistakes
Saying a local variable persists after the subroutine ends.
It is destroyed when the subroutine finishes.
Using global variables by default.
Local should be preferred; global only when genuinely needed everywhere.
Thinking two local variables with the same name conflict.
Each is separate within its own subroutine.
Giving only one reason for preferring local.
Several are examinable.
Exam tips
- Say scope when discussing where variables can be used.
- Give several reasons for preferring local variables.
- Note that local variables are destroyed when the subroutine ends.
- Link local variables to self-contained subroutines.
Key terms
- Scope
- Where in a program a variable can be accessed.
- Local variable
- A variable accessible only within its subroutine.
- Global variable
- A variable accessible throughout the program.
- Self-contained
- Not depending on code outside itself.
Related topics
Written and reviewed against the current AQA specification. Spotted an error? Let us know.