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 = 10
2 def reset():
3 score = 0
4 print("reset sees", score)
5 def add_bonus():
6 global score
7 bonus = 5
8 score = score + bonus
9 reset()
10 print("main sees", score)
11 add_bonus()
12 print("main sees", score)
13 print(bonus)

Variables

score (global)=10

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.

1 / 10

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.

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

Watch out: This is how Python handles scope. Not every programming language does it the same way, so don't assume the rule carries over word for word.

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.

5 of 5 still to sort.

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.

Computer Science · Scope

Same name, different variable

A Python program runs: total = 100, then a function spend() that contains just total = 40, then spend() is called, then print(total).

What does print(total) show? Pick the idea closest to yours.
How sure are you?

Why avoid globals?

?

Reason it through

If globals work, why do programmers generally see them as bad practice?

Link 1 of 4

First link · your turn

Think about who can get at a global variable. Just one function, or more?

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

Computer Science · Subroutines

Swap the global for a parameter

Right now add_bonus() uses global score to add 5 to score. Rebuild the program so no global is needed: the value goes in through a parameter, and the answer comes back with return.

  1. score = 10The main program keeps its own score, just like before.
  2. missing step
Which line is step 2?

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

1Scope is where a variable can be accessed and changed: global means anywhere in the program while it runs, local means only inside its own code block.
2In Python, assigning to a variable inside a function makes it local, unless the function first declares it global.
3A same-name local inside a function leaves the global untouched; the main program asking for a function's local variable gets an error.
4Globals are generally seen as bad practice: hard to track when changed elsewhere, and they make functions harder to test in isolation.
5Parameter passing gets data into a subroutine, and return passes a value back to the main program, so globals aren't needed.

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?
The section of the program where the variable can be accessed and modified.
What is a global variable?
A variable that can be accessed anywhere in the program, for the whole time the program is running.
What is a local variable?
A variable that can only be accessed and modified inside the code block where it was declared.
Do all programming languages handle scope in the same way?
No. Each language has its own rules; Python's rule is that a variable assigned inside a function is local unless it's declared global.
In Python, a function assigns x = 5. Is x automatically global?
No. It's local to that function, unless the function has specifically declared x as global.
How can a subroutine modify a global variable in Python?
Only if the program says so explicitly, by declaring the variable global inside that subroutine.
A subroutine creates a local variable with the same name as a global. What happens to the global's value?
Nothing. It's a separate variable, so the global's value hasn't changed.
What happens if the main program tries to access a variable that only exists inside a function?
An error is displayed, because that variable is only available inside the function.
Why are global variables generally seen as bad practice?
If a global is modified in other areas of the program, it can be difficult to track its value.
Why is a function that accesses global variables harder to work with?
It's harder to test in isolation, on its own.
What is parameter passing?
The process of passing data into a subroutine.
What does the return keyword do in a Python function?
It passes a value back to the main program.
How can you avoid using global variables?
Use functions, pass values in using parameters, and send results back with return.

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 OCR GCSE Computer Science topics

See the full OCR Computer Science curriculum →

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.