Test yourself on Variable Scope with exam style questions, marked instantly.
Flashcards that come back just before you would forget them.
A revision plan that remembers what you know and shows what to do next.
See which topics you have nailed and which need another look.
Quiz yourself on Variable ScopeGet your flashcardsBuild my revision planTrack my progressVideo Lesson
Craig'n'Dave: Local and Global VariablesWhere Variables Live
Scope is where a variable can be seen and used. A local variable is like your bedroom - only you can access it, and when you leave, it's cleaned up. A global variable is like the living room - everyone in the house can use it, and it's always there. Local variables exist only inside their function. Global variables exist everywhere. Generally, prefer local - it's tidier and safer!
Each function box is its own local scope, inside the global scope. Global variables are named by OCR and Edexcel. In OCR pseudocode a global variable is declared with the word global, for example global userID = "Cust001". AQA names local variables and why they are good practice.
Local vs Global
| Aspect | Local Variable | Global Variable |
|---|---|---|
| Declared | Inside a subprogram | Outside all subprograms |
| Accessible | Only within that subprogram | Anywhere in the program |
| Lifetime | Created when function runs, destroyed when it ends | Exists for entire program |
| Memory | Efficient - freed when not needed | Uses memory entire time |
| Safety | Can't be accidentally changed elsewhere | Can be changed from anywhere |
Deep Dive: Scope Example
global score = 0 // Global - accessible everywhere
function addPoints(points)
local bonus = 10 // Local - only in this function
score = score + points + bonus
endfunction
addPoints(5)
OUTPUT score // Works - score is global
OUTPUT bonus // ERROR - bonus doesn't exist hereMemory Trick
Local = Lives Locally (inside function only)
Global = Goes everywhere (whole program)
Exam Tips
- Best practice: Use local variables when possible
- Parameters: Are local to the function
- Common mistake: Trying to access local variable outside its function
Make this stick
Signing in gives access to all PrepWise features.