GCSE · Computer Science · OCR · Spec J277
Local and global variables
A function sets score to 0. The main program prints score straight afterwards and gets 10. Nothing's broken: there were two variables called score all along.
Computer Science · Programming
Which score just changed?
A short Python program with one global score and two functions. Step through one line at a time and watch the Variables panel: sometimes a second score appears, and it isn't the same one.
Before you press Next: reset() sets score to 0. What do you think line 10 will print?
1 score = 102 def reset():3 score = 04 print("reset sees", score)5 def add_bonus():6 global score7 bonus = 58 score = score + bonus9 reset()10 print("main sees", score)11 add_bonus()12 print("main sees", score)13 print(bonus)
Variables
Output
Step 1: Line 1 runs in the main program, outside both functions, so score is a global variable. Any part of the program can access it while the program runs.
Each Next runs one line. The Variables panel shows every variable that exists at that moment, and which part of the program it belongs to.
Computer Science · Scope
Local or global?
Pick each variable, then decide: is it local or global? Check where it's assigned before you choose.
Still to sort
Global (0)
Can be accessed anywhere in the program while it runs.
Where the line is: Assigned in the main program, or inside a function that has declared it global.
Local (0)
Can only be accessed and changed inside the function where it was created.
Where the line is: Assigned inside a function with no global line for that name.
A new program: a little game with a player, some lives and two functions. Each card tells you where its variable is assigned, and that is all you need to decide its scope.
Why avoid globals?
Reason it through
If globals work, why do programmers generally see them as bad practice?
First link · your turn
Think about who can get at a global variable. Just one function, or more?
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
Where a variable is created decides who can see it and who can change it. Get that wrong and your function quietly changes nothing, or your program stops with an error.
What you need to know
- A variable's scope is the part of the program where it can be accessed and changed.
- A global variable can be accessed anywhere in the program, for as long as the program runs.
- A local variable can only be accessed and changed inside the code block where it was declared.
Have a goYour friend is sure: "lives = 3 sits right at the top of my program, outside every function, so it's local to the top of the program." Are they right?
No. lives is global: any part of the program can access it.
It isn't inside any function, so it isn't trapped in one block. Your friend has mixed up where a variable is written with how far it can be seen.
- Languages don't all handle scope the same way. In Python, a variable assigned inside a function is local unless declared global.
- Want a subroutine to change a global? Say so, by declaring the variable global inside that subroutine.
Have a goA function contains lives = lives - 1, and you want it to change the global lives. What one line do you add at the start of that function?
global lives
A global can't be modified inside a subroutine unless the program says so explicitly, by declaring it global inside that subroutine.
- A local with the same name as a global is a separate variable, so the global's value hasn't changed.
- If the main program tries to use a variable that only exists inside a function, you get an error.
- Globals are generally seen as bad practice: if they're changed in other areas of the program, their values are hard to track.
- Functions that access global variables are also harder to test in isolation.
- Avoid globals by passing values in with parameters, and use return to send a value back to the main program.
Have a goA function triple(n) ends with return n * 3. The main program runs answer = triple(4). What ends up in answer?
12
4 is passed in through the parameter n, and return sends 12 back for the main program to store. Saying 4 forgets that the returned value is what gets stored.
The big picture
A variable's scope is the part of the program where it can be accessed and changed. A global variable can be accessed anywhere while the program runs; a local variable only inside the block where it was declared. In Python, a variable assigned inside a function is local unless it's declared global, so a same-name local leaves the global unchanged, and the main program gets an error if it tries to use a function's local variable. Globals are generally seen as bad practice because they're hard to track and make functions harder to test, so values are passed in with parameters and sent back with return.
Key points
Worked example
Problem
A Python program defines a function area(width, height) whose only line is result = width * height. The main program then runs area(4, 3), followed by print(result). What happens when it runs, and how would you fix it so it prints the area?
⚠ Watch out
Thinking that score = 0 inside a function changes the global score. In Python it creates a new local score, so the global keeps its old value unless the function declared it global first.
Memory hook
Two variables can share a name and still be strangers. Inside a function, a new assignment makes a new local, unless you say global first.
Check yourself
Cover the page. How is a local variable different from a global one? What extra line does a Python function need to change a global? And why do programmers usually avoid globals?
Flashcards
(13)What is the scope of a variable?
What is a global variable?
What is a local variable?
Do all programming languages handle scope in the same way?
In Python, a function assigns x = 5. Is x automatically global?
How can a subroutine modify a global variable in Python?
A subroutine creates a local variable with the same name as a global. What happens to the global's value?
What happens if the main program tries to access a variable that only exists inside a function?
Why are global variables generally seen as bad practice?
Why is a function that accesses global variables harder to work with?
What is parameter passing?
What does the return keyword do in a Python function?
How can you avoid using global variables?
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 OCR GCSE Computer Science topics
- Data type: Character and string
- Data type: Integer
- Data type: Real
- File handling: open, read, write, close
- Inputs, outputs, assignments
- Iteration (count- and condition-controlled loops)
- Operators (arithmetic and Boolean)
- Random number generation
- Records
- Sequence
- SQL: SELECT, FROM, WHERE
- Sub programs: functions
How this lesson was checked. This OCR GCSE Computer Science (specification J277)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 3 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.