Software Design & Development · Implementation

SDD9 — Scope: Local vs Global

📅 Thu 25 Jun 2026 · P3+P4 (double)
~90 minutes
Learning intentions
Success criteria
Warm up — recap from SDD8
Answer all three questions, then check your answers.
Question 1
What is the term for the name declared inside a sub-program's own definition to represent one of its inputs?
Question 2
Given def calculateChange(amountPaid, price): return amountPaid - price, what does calculateChange(100, 75) return?
Question 3
Actual parameters are matched to formal parameters by...

Key vocabulary

Scope
The region of a program in which a particular variable can be accessed.
Local variable
A variable created inside a sub-program, which only exists and can only be accessed within that sub-program.
Global variable
A variable declared outside all sub-programs, which can be read from anywhere in the program, including inside every function.
Local scope
The area inside a single sub-program where its own local variables exist — they are created when it starts running and destroyed when it ends.
Global scope
The area of the whole program, outside every sub-program, where global variables exist for the entire time the program runs.

Where a variable can and cannot be seen

What is scope?

Scope is the region of a program in which a particular variable can be accessed. Every variable belongs to one of two scopes: local scope, tied to a single sub-program, or global scope, spanning the whole program. Understanding scope means being able to say, for any given variable, exactly which lines of code are allowed to read or change it — and just as importantly, which lines are not.

Local variables and local scope

A local variable is created inside a sub-program — either as a formal parameter (as covered in SDD8) or as a variable assigned somewhere in the sub-program's own body. A local variable exists only for as long as that sub-program is running: it is created the moment the sub-program is called, and it is destroyed the moment the sub-program finishes and returns control to whatever called it. This means a local variable's name has no meaning anywhere else in the program — not in the main part of the program, and not inside any other sub-program, even one that runs immediately afterwards.

Global variables and global scope

A global variable is declared outside every sub-program, directly in the main part of the program. Because it exists in global scope, it can be read from anywhere in the program, including from inside any function or procedure, without any special syntax. This is different from local variables, which are invisible outside their own sub-program.

Reading a global variable versus modifying one

Reading a global variable's value from inside a function works with no extra steps. However, changing (reassigning) a global variable from inside a function requires the variable to be declared with the global keyword first, inside that function. Without it, Python assumes any variable that is assigned a new value anywhere inside a function must be a new local variable — and if that same name is also read before being locally assigned within the same function, this produces a runtime error, because Python has already decided the name refers to a not-yet-created local variable rather than the existing global one.

Why this connects to SDD8

Formal parameters, introduced in SDD8, are a special case of local variables: each formal parameter is created fresh every time its sub-program is called, exists only inside that call, and is destroyed once the sub-program finishes — exactly like any other local variable. This is also why giving a formal parameter the same name as a global variable does not cause a clash: inside the sub-program, the local (parameter) version simply takes priority for that name.

Global scope — whole program
discountRate totalSales
Local scope — applyDiscount()
price discountedPrice
Local scope — recordSale()
price
Both functions can read discountRate/totalSales. Neither function's local variables exist anywhere outside their own box.

Worked examples

Example 1 — A local variable does not exist outside its function
1
discountRate = 0.1

def applyDiscount(price):
    discountedPrice = price - (price * discountRate)
    return discountedPrice

applyDiscount(75)
print(discountedPrice)
2
discountedPrice is a local variable — it is created inside applyDiscount and only exists while that function is running. discountRate is global, declared outside every function.
applyDiscount(75) returns 67.5 correctly. But the final print(discountedPrice) line raises NameError: name 'discountedPrice' is not defined, because discountedPrice was destroyed the moment applyDiscount finished running.
Example 2 — Reading a global variable works; modifying one needs the global keyword
1
totalSales = 0

def recordSale(price):
    global totalSales
    totalSales = totalSales + price

recordSale(75)
recordSale(60)
print(totalSales)
2
The line global totalSales tells Python that, inside this function, totalSales refers to the existing global variable rather than a new local one — this is what allows the reassignment totalSales = totalSales + price to update the global value permanently.
After recordSale(75) then recordSale(60), totalSales is 135. Without the global keyword, this exact code would raise UnboundLocalError: local variable 'totalSales' referenced before assignment, because Python would treat totalSales inside the function as a new local variable that has not yet been given a value.
Example 3 — Local variables with the same name in different functions do not clash
1
def functionA():
    total = 10
    return total

def functionB():
    total = 20
    return total
2
Both functions use a local variable called total. Because each has its own separate local scope, these are two completely independent variables that just happen to share a name — neither function is aware the other's total exists.
functionA() returns 10 and functionB() returns 20, in either order, with no interference between them.
Now you try
itemCount = 5

def sellItem():
    itemCount = itemCount - 1
    return itemCount

sellItem()
Predict: what happens when sellItem() is called, and explain why in terms of scope.
⚠️ Common mistakes — examiner feedback
📝 Exam tip

When asked to explain a scope-related error, name the specific rule being broken — "this is a local variable being accessed outside its sub-program" or "this global variable is being modified without the global keyword" — rather than a vague answer like "the variable doesn't work here." Examiners are looking for the precise scope concept, not just recognition that something is wrong.

Task Set A — Core questions

Task Set A — Core questions
Work through all questions, then check your answers.
Question 1
What term describes a variable that only exists and can only be accessed inside the sub-program where it was created?
Question 2
What term describes a variable declared outside all sub-programs, which can be read from anywhere in the program?
Question 3
discountRate = 0.1

def applyDiscount(price):
    discountedPrice = price - (price * discountRate)
    return discountedPrice
Which variable in this code is local?
Question 4
Using the same code as Question 3, what happens if print(discountedPrice) is written after the function has finished running?
Question 5
Which keyword must be used inside a function before a global variable can be reassigned there?
Question 6
totalSales = 0

def recordSale(price):
    global totalSales
    totalSales = totalSales + price

recordSale(75)
recordSale(60)
What is the value of totalSales after both calls?
Question 7
Using the same code as Question 6, what would happen if the line global totalSales were removed?
Question 8
Write Python code that declares a global variable stockLevel set to 40, then defines a function reduceStock(amount) that correctly subtracts amount from the global stockLevel (remember the keyword needed to modify a global variable inside a function). Then call reduceStock(5).
stockLevel = 40

def reduceStock(amount):
    global stockLevel
    stockLevel = stockLevel - amount

reduceStock(5)
Question 9
Explain the difference between a local variable and a global variable, using discountRate and discountedPrice from Example 1 as your evidence. (3 marks)
Question 10
Explain why reading a global variable inside a function needs no special keyword, but modifying one does. (3 marks)

Task Set B — Extension

Task Set B — Extension · Beyond the specification
Longer written answers — no auto-check. Discuss your answers with your teacher.
Extension 1
SDD18 will introduce maintainability as an evaluation criterion. Suggest why a program that relies heavily on global variables (rather than parameters and return values) might be considered harder to maintain.
Extension 2
Suppose a global variable and a formal parameter share the same name, e.g. a global price and a function def showPrice(price):. Predict which value is used inside the function, and explain whether this affects the global variable at all.
Extension 3
SDD17 will cover trace tables for debugging. Suggest how a trace table might need to represent the difference between a global variable and a local variable across multiple function calls.
📁 File this in OneNote under:
Higher Computing Science → Software Design & Development → SDD9
📌 Teacher notes — not for pupils

Double period — this topic pairs naturally with SDD8 and benefits from live demonstration of the UnboundLocalError, which pupils otherwise find abstract.

Suggested timing: 5 min warm-up + vocab · 20 min notes + scope diagram (live-run Example 1's NameError and Example 2's UnboundLocalError in PyCharm/REPL so pupils see the actual tracebacks) · 15 min Example 3 + "now you try" · 40 min Task Set A · Task Set B as homework or extension for early finishers.

Key misconception: pupils often think removing the global keyword just means the change "doesn't happen" silently — it is worth stressing this is a runtime error (UnboundLocalError), not silent failure, since exam mark schemes expect the specific error behaviour.

SQA command words covered: describe, explain, identify, predict.

Extension suggestion: Extension 1 (maintainability) is a light preview of SDD18 — if time is tight, this can be moved to the SDD18 warm-up instead of being covered here.