GCSE · Computer Science · AQA · Spec 8525
Local variables
You give a variable a value, and three lines later the program swears it doesn't exist. It's telling the truth. So where did it go?
Computer Science · Watch the variables
Now you see it, now you don't
A tiny game program. Step through it one line at a time and keep your eyes on the Variables panel: it shows every variable that exists right now, and whether it's global or local.
Predict before you press Next: once level_score has finished, will the main program still be able to use points?
1 bonus = 52 def level_score(coins):3 points = coins * 10 + bonus4 return points5 total = level_score(3)6 total = total + level_score(2)7 print(total)
Variables
Output
Step 1: bonus is a global variable in this program: it lasts for the whole run and any part of the program can use it.
Use Next and Back to move through the run. Stop on step 4, then step 6, and compare the panel. That's the moment a local variable disappears.
Why bother with local variables?
Reason it through
Why is it good practice to use local variables in a subroutine?
First link · your turn
A local variable can only be accessed from inside its own subroutine. So what can the rest of the program NOT do to it?
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
Why a variable made inside a subroutine vanishes the moment the subroutine ends, and why that's actually a good thing.
What you need to know
- A subroutine is a named 'out of line' block of code. You call it just by writing its name in a program statement.
- Variables that a subroutine declares for itself are called local variables.
- A local variable usually only exists while its subroutine is running: born when it's called, gone when it finishes.
Have a goA subroutine with one local variable gets called three times in a row. How many separate copies of that local variable get created over the whole run?
Three: one fresh copy per call, and each is gone before the next call starts.
It's tempting to picture one variable that's reused, but a local variable usually only exists while its call is running, so every call starts a new one.
- It's usually only accessible inside that subroutine, so the main program and other subroutines can't reach it.
- Scope is the word for where in a program a variable can be accessed.
- Global variables are the opposite: accessible anywhere in the program, for the whole time it runs.
- A variable made in a subroutine is local unless it's specifically set as global, even when it shares a name.
Have a goYour friend Sam is certain: "My subroutine runs total = 0, so the main program's total is now 0 too. Same name, same variable!" Is Sam right?
No. The subroutine's total is its own local variable; the main program's total is untouched.
A shared name links nothing. A variable made in a subroutine is local unless it's specifically set as global, so Sam has two separate variables.
- Need a subroutine's answer outside it? Return the value, and store what comes back in a variable.
Have a gofind_max(a, b) stores the larger number in a local variable called biggest and returns it. Fill the gaps so the main program prints 9: ___ = find_max(4, 9), then print(___)
Any new variable name in both gaps, e.g. largest = find_max(4, 9), then print(largest). Even biggest works here.
What reaches the main program is the returned value, caught in a main-program variable. Calling that variable biggest just makes a separate variable with the same name.
- Local variables are good practice because nothing else can accidentally change them.
- Structured programs split work into subroutines with clear, well-documented interfaces: parameters, local variables and return values.
The big picture
A local variable is a variable declared inside a subroutine. It usually only exists while that subroutine is running, and it can usually only be accessed from inside it: its scope is the subroutine. A global variable can be accessed anywhere in the program for the whole run, and a variable made in a subroutine is local unless it's specifically set as global. Using local variables is good practice, because nothing else can accidentally change them, and they sit alongside parameters and a return value as part of the subroutine's clear interface.
Key points
Worked example
Problem
A Python program works, but its subroutine changes a global variable. The main program starts with price = 0. The subroutine work_out_price(items) has two lines: global price, then price = items * 3. The main program then runs work_out_price(5) and print(price). Rewrite it so the subroutine uses a local variable and a return value instead, and say what the new version prints.
⚠ Watch out
Using a subroutine's local variable in the main program after the call. It feels fine, because the variable was right there a moment ago. But it no longer exists, so the program fails. Return the value and store it instead: total = ticket_cost(3), then use total.
Memory hook
What's made in the subroutine stays in the subroutine, and it's gone when the subroutine ends. Need the value outside? Return it and catch it.
Check yourself
A subroutine called area runs width = 12. Is width local or global? After area finishes, can the main program print width? If it needs the value, what should area do?
Flashcards
(12)What is a subroutine?
What is a local variable?
When does a local variable exist?
What does the scope of a variable mean?
What is a global variable?
A variable is created inside a subroutine and nothing else is said about it. Local or global?
A subroutine and the main program both have a variable called result. How many variables is that?
How does the main program get hold of a value worked out in a subroutine's local variable?
A subroutine is called twice. Does its local variable remember its value from the first call?
Why can't other parts of a program accidentally change a local variable?
What do a subroutine's local variables, parameters and return value make up?
What is the structured approach to programming?
Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.
Learning with Lightbulb is opening soon
You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.
Keep me postedMore AQA GCSE Computer Science topics
How this lesson was checked. This AQA GCSE Computer Science (specification 8525)lesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 30 October 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.