GCSE · Computer Science · Eduqas · Spec C500QS
Local and global variables
A function sets score to 0, yet the main program still prints 10. Broken? No. That's scope, and you'll soon predict it every time.
Computer Science · Scope
Run it in your head: which score is it?
Python again, because not every language handles scope the same way.
Call each marked step before the table shows it. Nothing is run: every value is written by the lesson.
score = 10def show():print(score)def reset():score = 0print(score)def bonus():global scorescore = score + 5show()reset()print(score)bonus()print(score)
| global score | local score |
|---|---|
| 10 | — |
Step 1. Worked for you. score = 10 sits outside every subroutine, so it is a global variable. No local score exists yet.
Watch the two columns. Which variable is each line really touching?
Computer Science · Scope
Why can't the main program see that variable?
Press Run. Read what Python says and find the line it points at. Then choose the one line to change so the program prints a value.
- def add_bonus(score):
- bonus_score = score + 5
- return bonus_score
- add_bonus(10)
- print(bonus_score)
Messages and outputs are what Python 3 prints for these programs (the traceback header is left out).
Why keep variables local?
Reason it through
Why does keeping variables local cut down on mistakes?
First link · your turn
A global variable is available everywhere. So which parts of the program are able to change it?
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
Where you create a variable decides who can see it, who can change it, and how long it lasts.
What you need to know
- Scope is the part of a program where a variable can be accessed and modified.
- A local variable is defined and visible only in a specific part of a program, such as one subroutine.
- A global variable is available in all parts of a program, even inside subroutines, for as long as the program runs.
- A local variable is created when its subroutine is called, and its value is lost when the subroutine ends.
Have a goSam says: 'I made a variable inside my function. Once the function finishes, it's still sitting there for the main program to grab.' Is Sam right?
No. The variable's value is lost when the subroutine ends.
Sam pictures the variable as sticking around, but a local variable is only created when its subroutine is called and is gone when it ends.
- Not all programming languages handle scope the same way, so the rules here are Python's.
- In Python, a variable is local unless you declare it global. Assigning inside a function doesn't make it global.
- Assigning to a global's name inside a subroutine creates a new local variable. The global's value does not change.
Have a goThe main program sets total = 5. A Python subroutine runs total = 99 on its first line, then ends. What does the main program's total hold?
5
Setting total inside the subroutine made a new local variable, so it did not touch the global.
- To change a global from inside a subroutine, declare it with the global keyword. The main program then sees the change.
Have a gobonus() has to change the global score. Which goes first inside it: score = 5, global score, or return score?
global score
Without that declaration, Python treats assigning to score as making a new local variable, and the global would not change.
- If the main program uses a variable declared only inside a subroutine, an error occurs.
- Instead of globals, pass values in as parameters and send results back with return. Programmers generally see globals as bad practice.
The big picture
Scope is the part of a program where a variable can be accessed and modified. A local variable belongs to one part of a program, such as a subroutine; a global variable is available everywhere. In Python, assigning to a name inside a function makes a new local unless the function declares it global. Programmers prefer local variables, with values passed in as parameters and back with return.
Key points
Worked example
Problem
This program prints 15, but add_points() only works because a global variable called total exists. total = 10 def add_points(): global total total = total + 5 add_points() print(total) Rewrite it so the subroutine gets its data through a parameter and sends its result back with return. Check it still prints 15.
⚠ Watch out
Thinking that assigning to a variable inside a subroutine changes the global of the same name. In Python it creates a new local variable, and the global keeps its old value unless the subroutine declares the variable global.
Memory hook
Same name, different box. Inside a function, score = 0 opens a brand-new local box. The global box stays on the shelf untouched, unless you say global.
Check yourself
Main has x = 1. A function runs x = 2, then ends. What does main see for x, and which line would change that?
Flashcards
(14)What is the scope of a variable?
What is a local variable?
What is a global variable?
When does a local variable exist?
In Python, a function runs x = 5 and the main program also has a global x. What does that line create?
In Python, how does a subroutine change a global variable instead of making a new local?
A subroutine only prints a global variable and never assigns to it. Does it need the global keyword?
What happens if the main program uses a variable declared only inside a subroutine?
How does the main program get a value calculated inside a function?
What is parameter passing?
Give two risks of global variables.
Name two ways keeping variables local reduces mistakes.
Why does using parameters and return make a subroutine easier to reuse?
Is scope handled the same way in every programming language?
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 Eduqas GCSE Computer Science topics
How this lesson was checked. This Eduqas GCSE Computer Science (specification C500QS)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 16 November 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.