Scope & arguments
Local variables
- A variable made inside a function is local.
- Its name belongs to that function call.
- Code outside cannot access that local name directly. An object returned by the function can still be used after the call ends.
def make_message():
note = "secret"
print(note)
make_message()
# print(note) would be an error: note is local
Global variables
- A variable made outside all functions is global.
- A function can read a global value.
- To reassign a global name inside a function you need
global. Mutating a shared list withappenddoes not reassign its name and does not needglobal.
Default values
- A parameter can have a default value:
def f(x, step=1):. - If the caller leaves it out, the default is used.
- Parameters with defaults come after the normal ones.
def power(base, exp=2):
return base ** exp
print(power(5))
print(power(2, 3))
How arguments are passed
- Each parameter is a local name bound to the object passed by the caller. Python does not automatically copy that object.
- Assignment such as
n = n + 100binds the local name to a different object. It does not reassign the caller's name. - Mutating a list with
appendchanges the shared object. Assigning a new list to the parameter only changes the local binding.
def try_change(n):
n = n + 100 # rebinds the local name; no object copy is implied
print(n)
x = 5
try_change(x)
print(x) # x is still 5
Trace mutation and reassignment
Predict both printed lists before running this program:
def revise(items):
items.append("B")
items = ["C"]
first = ["A"]
second = first
revise(first)
print(first)
print(second)
Both lines print ['A', 'B']. first, second and the parameter initially refer to one list. append changes it. The next assignment makes only items refer to a new list; it leaves first and second pointing at the original list.
Make a new result without changing the input
To preserve a caller's list, copy its outer structure and modify the new list:
def with_item(items, value):
result = items.copy()
result.append(value)
return result
original = [4]
updated = with_item(original, 9)
print(original) # [4]
print(updated) # [4, 9]
This is a shallow copy: nested mutable items would still be shared. Our task uses flat lists. In the next task, repair a function that reassigns its parameter when it should mutate the caller's list; then apply the opposite requirement to a new input.
In Cambridge pseudocode (A-Level 9618)
BYVALgives the procedure a parameter value;BYREFpermits assignment to the caller's variable. Python's shared-list mutation is not the same as CambridgeBYREFparameter reassignment.- ⚠️ These two keywords are A-Level 9618 only. IGCSE 0478 passes every parameter by value; to send a result back you use a
FUNCTIONandRETURNit.
PROCEDURE Increase(BYREF N : INTEGER)
N ← N + 1
ENDPROCEDURE
PROCEDURE Show(BYVAL N : INTEGER)
OUTPUT N
ENDPROCEDURE
Common mistakes
- A variable made inside a function is local — it cannot be seen outside.
- Arguments fill the parameters in the order you write them.
- To use a result outside,
returnit and store what comes back.
Now you try
- The checks call your function with different arguments.
- Press Check answer to test your code.
Arguments live in the call
Each call has its own local parameter names; mutable argument objects can be shared.
Difficulty 3/5 · 200 coins + a small bonus on first completion today
Write greet(name, greeting="Hi") that returns greeting, then , , then name, then !. So greet("Sam") is Hi, Sam! and greet("Sam", "Hello") is Hello, Sam!.
Click Run to see the output here.
Difficulty 3/5 · 200 coins + a small bonus on first completion today
Write add_item(lst, x) that appends x to the list lst. Do not return anything — the caller's list should change because lists are mutable.
Click Run to see the output here.
Difficulty 3/5 · 200 coins + a small bonus on first completion today
Write with_item(items, value) for a flat list. Return a new list containing the original items followed by value. Keep the input list unchanged. For example, [4] and 9 produce [4, 9], while the caller's list stays [4].
Click Run to see the output here.
Difficulty 3/5 · 200 coins + a small bonus on first completion today
Write between(low, high, x) that returns True if x is from low to high (inclusive), otherwise False.
Click Run to see the output here.
Difficulty 3/5 · 200 coins + a small bonus on first completion today
Write make_countdown(start, stop=1) that returns a list counting down from start to stop. So make_countdown(3) is [3, 2, 1] and make_countdown(5, 3) is [5, 4, 3].
Click Run to see the output here.