Scope describes where in a program a variable can be used. A global variable belongs to the main program. A local variable belongs to one subroutine and stops existing when that subroutine ends.
This lesson sits in the programming structure module. It builds on choosing a data type and prepares you for building a subroutine. Exam questions ask you to state whether a variable is local or global, trace a program that uses both, or explain why local variables are good practice.
How do I tell local from global?
Look at where the variable is declared.
- Declared in the main program, outside every subroutine: global. Every part of the program can read and change it.
- Declared inside a subroutine: local. Only that subroutine can use it, and it is created fresh each time the subroutine is called.
- A parameter in the subroutine’s brackets also behaves as a local variable for that subroutine.
Think of a whiteboard in a classroom (global) and a page in your own notebook (local). Anyone can write on the whiteboard. Only you use your notebook page, and a new page is started for each lesson.
Worked example
Trace this program and state the output.
DECLARE Total : INTEGER
Total ← 0
PROCEDURE AddPoints(Points : INTEGER)
DECLARE Bonus : INTEGER
Bonus ← 5
Total ← Total + Points + Bonus
ENDPROCEDURE
CALL AddPoints(10)
CALL AddPoints(20)
OUTPUT Total
Here Total is global. Points and Bonus are local to AddPoints.
| Step | Points | Bonus | Total |
|---|---|---|---|
| Start | not created | not created | 0 |
| CALL AddPoints(10) | 10 | 5 | 0 + 10 + 5 = 15 |
| CALL AddPoints(20) | 20 | 5 | 15 + 20 + 5 = 40 |
| After the calls | gone | gone | 40 |
The output is 40. Bonus is created again at each call, so the second call starts with a new Bonus of 5, not a leftover value. Total survives because it is global.
Which mistake catches students most often?
Mistaken answer: adding
OUTPUT Bonusat the end of the program, after the two calls.This is an error.
Bonusis local toAddPointsand no longer exists once the procedure has finished. The main program cannot see it.
There are two correct ways to get a value out of a subroutine. Output it inside the subroutine, or have a function return it. Returning is covered in tracing a returned value.
A second mistake is thinking a local variable with the same name as a global changes the global. If a procedure declares its own Total, the global Total is untouched.
Self-check
1. In the example above, state which variables are local and which are global.
Show answer
Global: Total. Local to AddPoints: Points and Bonus.
2. State the output.
X ← 3
PROCEDURE Test()
DECLARE X : INTEGER
X ← 8
ENDPROCEDURE
CALL Test()
OUTPUT X
Show answer
The procedure declares its own local X, set to 8. The global X is never changed, so the main program outputs 3.
3. Give one reason why a programmer prefers local variables.
Show answer
A local variable cannot be changed by accident from elsewhere in the program, so errors are easier to find. Another valid reason: the same name can be reused in different subroutines without clashing.
Where does this lead next?
Scope tells you what a subroutine can see. Next, build a subroutine with clear parameters, so it receives what it needs instead of reaching into globals. Use the restricted pseudocode trace trainer to practise filling in the table above, then check the same logic in the safe Python reasoning sandbox.
If you want someone to watch your tracing and stop you at the first wrong row, that is what online one-to-one Computer Science tuition is for.