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 = 5
2 def level_score(coins):
3 points = coins * 10 + bonus
4 return points
5 total = level_score(3)
6 total = total + level_score(2)
7 print(total)

Variables

bonus (global)=5

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.

1 / 9

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.

Step 1 of 9: 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..

Watch out: The variables panel is the whole story here. If a variable isn't in the panel, it doesn't exist at that moment, so no line of code can use it.

Computer Science · Same name, different variable

Two variables called result

A Python subroutine called double_it(n) has two lines inside it: result = n * 2, then return result. The main program runs three lines: result = 7, then answer = double_it(10), then print(result). So the main program and the subroutine both use a variable called result.

What will print(result) display? Pick the idea closest to what you think right now.
How sure are you?

Local vs global

Local variablevsGlobal variable

Start with the first row. Where a variable can be accessed is the difference people mix up most.

Focus

Where it can be accessed

Local variable

Usually only from inside the subroutine that created it

Global variable

Anywhere in the program: the main program and every subroutine

The insight

This is what scope means: the scope of a variable determines where it can be accessed. A local variable's scope is its own subroutine; a global variable's scope is the whole program.

How long it exists

Local variable

Usually only while its subroutine is running

Global variable

For the whole time the program is running

How it comes about

Local variable

Created inside a subroutine. It's local unless it's specifically set as global

Global variable

Created in the main program (in Python), or specifically set as global inside a subroutine

Computer Science · Spot the scope bug

Which line breaks the program?

This Python program should print the cost of 3 cinema tickets at 8 pounds each. Instead of printing You pay 24, it crashes. Pick the line that causes the crash.

A student's program — which line goes wrong?

Why bother with local variables?

?

Reason it through

Why is it good practice to use local variables in a subroutine?

Link 1 of 4

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?

2
Locked — reveal the link above first
3
Locked — reveal the link above first
4
Locked — reveal the link above first

Computer Science · Write it, then mark it

Put it into words

Describe how local variables, parameters and return values help a programmer follow a structured approach to programming. [4 marks]

0 words · your answer stays on this page and is not sent anywhere.

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

1A subroutine is a named 'out of line' block of code. You run (call) it just by writing its name in a program statement.
2Subroutines can declare their own variables. These are called local variables.
3A local variable usually only exists while its subroutine is running: it's created when the subroutine is called and it's gone when the subroutine finishes.
4A local variable is usually only accessible from inside its subroutine. The main program and other subroutines can't use it.
5The scope of a variable determines where in the program it can be accessed.
6A global variable can be accessed anywhere in the program, for the whole time the program is running.
7A variable created inside a subroutine is local unless it's specifically set as global. A local variable can share its name with another variable without affecting it.
8To use a subroutine's result elsewhere, return it and store the returned value in a variable. Don't try to reach the local variable after the call.
9Local variables are good practice: other parts of the program can't accidentally change them, so each subroutine is self-contained, easier to test and easier to reuse.
10In the structured approach to programming, a program is split into subroutines, each with a clear, well-documented interface: its parameters, its local variables and its return value.

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?
A named 'out of line' block of code that runs when it is called by writing its name in a program statement.
What is a local variable?
A variable declared inside a subroutine. It is usually only accessible from inside that subroutine.
When does a local variable exist?
Usually only while its subroutine is running: it's created when the subroutine is called and gone when it finishes.
What does the scope of a variable mean?
Where in the program the variable can be accessed.
What is a global variable?
A variable that can be accessed anywhere in the program for the whole time the program is running.
A variable is created inside a subroutine and nothing else is said about it. Local or global?
Local. A variable is local unless it is specifically set as global.
A subroutine and the main program both have a variable called result. How many variables is that?
Two separate variables. Changing the subroutine's local result has no effect on the main program's result.
How does the main program get hold of a value worked out in a subroutine's local variable?
The subroutine returns the value, and the main program stores the returned value in its own variable.
A subroutine is called twice. Does its local variable remember its value from the first call?
Normally no. A new local variable is created on each call, and the old one disappeared when the first call finished.
Why can't other parts of a program accidentally change a local variable?
Because a local variable can only be accessed from inside its own subroutine.
Why do local variables make a subroutine easier to reuse?
The subroutine is self-contained: it relies only on its parameters and return value, not on variables elsewhere in the program.
What is the structured approach to programming?
Breaking a program into modules (subroutines), each with a clear, well-documented interface made up of its local variables, parameters and return values.

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 posted

More AQA GCSE Computer Science topics

See the full AQA Computer Science curriculum →

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.