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 small game program. Step through it one line at a time and keep your eyes on the Variables panel: it shows every variable that exists at that moment, 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 created in the main program, outside any subroutine. In Python that makes it a global variable: 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 and 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 a good thing.
What you need to know
- What a subroutine is, and what it means to call one
- What a local variable is, where it can be accessed and how long it exists
- What the scope of a variable means
- How a local variable differs from a global variable
- How to get a value out of a subroutine without using its local variable
- Why using local variables is good practice in a structured program
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, so each subroutine is self-contained, with parameters in and a return value out.
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
Trying to use a subroutine's local variable in the main program after the call has finished. By then the variable no longer exists, so the program fails. The fix is to return the value and store it: 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. If you 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?
Why do local variables make a subroutine easier to reuse?
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 29 September 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.