Learning route
Give each subprogram one job. Pass in data. Return a useful result.
Prerequisite: Can use variables, decisions and loops. Can read an indexed list. Follow Lessons 01–03 first if these skills need repair.
- Decompose a program into named subprograms.
- Distinguish a parameter, an argument and a returned value.
- Explain why printing a value is different from returning it.
- Trace local variables and test a function independently.
Retrieve and explain
- What is the value of
3 * 4 + 2? - Why might repeating the same calculation in five places make changes harder?
- What does
len([4, 8, 9])return? - Explain the difference between displaying a value and assigning it to a variable.
Decomposition divides a problem into smaller responsibilities. A function names a reusable calculation and returns its result. The caller can store that result, combine it with another result or display it.
def area(width, height):
result = width * height
return result
floor_area = area(7, 3)
print(floor_area)
| Term | In this example | Meaning |
|---|---|---|
| Definition | def area(width, height): | Describes the subprogram; does not yet run its body. |
| Parameter | width, height | Local names which receive values when called. |
| Argument | 7, 3 | Values supplied by this call. |
| Return value | 21 | Data sent back to the caller. |
| Local variable | result | Belongs to this call of area. |
def welcome():
print("Welcome to the session")
welcome()
Here welcome is used as a procedure: it performs an action and has no explicit return. In Python it still returns None implicitly. A function definition with parameters is not a call; the body runs only when the call is reached.
Scope is about where a name is available.
A name assigned inside a function is normally local to that function. A global name is defined at module level. Prefer passing required data as arguments and returning results, rather than changing global state. Different calls have their own local values.
Worked example
A workshop charges £3 per participant plus a £2 booking fee. The function returns a whole-number cost. The global variable named fee is deliberately different from the local one to make scope visible. Assume a non-negative integer group size.
fee = 99
def booking_cost(people):
fee = 2
cost = people * 3 + fee
return cost
price = booking_cost(4)
print("Price:", price)
print("Global fee:", fee)
| Step | Local state | Main-program state |
|---|---|---|
| Before call | No call yet | fee = 99 |
| Call booking_cost(4) | people = 4, fee = 2 | fee remains 99 |
| Calculate | cost = 14 | price not assigned yet |
| Return | Returns 14 | price = 14, fee = 99 |
Output: Price: 14, then Global fee: 99. Assigning the local fee did not replace the global fee. In normal code, distinct meaningful names would make this easier to read.
Repair practice
The learner wants to store the result and add 2. Explain the failure and repair the function.
def double(number):
print(number * 2)
answer = double(5)
print(answer + 2)
What appears before the error? What is stored in answer? Keep the final output in the main program.
Repair feedback
def double(number):
return number * 2
answer = double(5)
print(answer + 2)
The original function first displays 10, but returns None implicitly. Therefore answer is None and adding 2 raises a TypeError. The repaired function returns 10 and the main program displays 12. Do not keep the extra print inside the function unless it is an explicit requirement.
Independent practice
Independent build: activity points. Write session_points(minutes). It returns 4 points for every complete 15-minute block. A session of at least 60 minutes also earns a bonus of 5 points. Assume minutes is a non-negative integer.
- Calculate the points inside the function using its parameter, not a global input variable.
- Return the points; do not print inside the function.
- In the main program, read two session lengths and call the same function for each.
- Add the returned results and output Total points: followed by the sum.
- Test the function with 0, 14, 15, 59 and 60. Test the full program with (15, 60), (0, 14) and (59, 60).
Use integer division to count complete blocks. For example, 29 minutes contains one complete 15-minute block.
# Proof Academy | Lesson 04 | Activity points
# Inputs are non-negative whole numbers of minutes.
def session_points(minutes):
# Your task: 4 points per complete 15-minute block.
# Your task: add 5 bonus points if minutes is at least 60.
# Your task: return the result instead of printing it.
pass
# Your task: read two session lengths, call the function twice,
# and print their combined points.
Model program and marking guidance
# Proof Academy | Lesson 04 | Worked solution
def session_points(minutes):
points = (minutes // 15) * 4
if minutes >= 60:
points = points + 5
return points
first_minutes = int(input("First session minutes: "))
second_minutes = int(input("Second session minutes: "))
total_points = session_points(first_minutes) + session_points(second_minutes)
print("Total points:", total_points)
- Defines session_points with a minutes parameter. [1]
- Uses complete 15-minute blocks, not ordinary division. [1]
- Multiplies the block count by 4. [1]
- Adds exactly 5 bonus points for minutes >= 60. [1]
- Returns the result without printing from the function. [1]
- Main program reads two integers and uses two calls to the same function. [1]
- Adds the returned values and displays a labelled total. [1]
- Records expected and actual results for the five function tests and three complete-program tests. [1]
Exam practice
Original Proof Academy practice · 6 marks. Trace the following code.
value = 10
def increase(value):
value = value + 3
return value
new_value = increase(4)
print(value, new_value)
- Name the parameter and identify the argument in this call. [2]
- State the two printed values in order. [2]
- Explain why the global value does not change. [1]
- Give one specific maintenance benefit of using a function for a repeated calculation. [1]
Worked feedback
Parameter: value (1); argument: 4 (1). Printed values: 10 (1), 7 (1). The assignment inside increase changes its local parameter, not the module-level name (1). A shared calculation can be corrected in one definition instead of editing several copies (1); accept another specific maintenance benefit.
Homework and feedback
30 minutes: 5 minutes retrieval, 15 minutes coding, 10 minutes tests.
Write delivery_cost(weight) for a whole-number weight from 1 to 20 inclusive. Return 3 for weights 1–5 and 6 for weights 6–20. Return −1 for an out-of-range weight. Read a weight in the main program, call the function and display either Invalid weight or a labelled cost. Test 0, 1, 5, 6, 20 and 21. Keep input and print outside the function.
Homework feedback
def delivery_cost(weight):
if weight < 1 or weight > 20:
cost = -1
elif weight <= 5:
cost = 3
else:
cost = 6
return cost
weight_input = int(input("Weight: "))
charge = delivery_cost(weight_input)
if charge == -1:
print("Invalid weight")
else:
print("Cost:", charge)
Returns in test order: −1, 3, 3, 6, 6, −1. The sentinel return value is interpreted by the caller. Accept equivalent conditions and clear output wording.
Sources and further practice
This chapter is written for Pearson Edexcel GCSE Computer Science 1CP2. Use the specification references in the chapter heading to locate the relevant requirements. Practice questions and point allocations on this page are original Proof Academy teaching material unless a question is explicitly identified as a sourced past-paper bridge.
- Pearson Edexcel 1CP2 specification
- PMT Paper 1 papers and mark schemes
- PMT Paper 2 papers and mark schemes
When using an original paper, open its matching mark scheme and supplied coding files where required. Use that paper's instructions and marks. A short classroom exercise is not a substitute for a full official task.
Trace lab · inline, no inner scroll
Booking cost and scope
booking_cost returns people × 3 + 2. The global fee stays 99.
Keep your evidence
Export your notes before changing devices.
Notes stay in this browser. The Quiz tab records the chapter check, and Resources holds the long-answer questions.