All subjects · Computer Science · WCP02 Practical Programming and Problem-solving
Subprograms: functions, procedures, parameters and arguments
Edexcel IAL Computer Science · WCP02 Practical Programming and Problem-solving
Subprograms package code under a name so it can be reused, tested on its own and kept short. The exam expects at least one user-defined subprogram with parameters.
Procedures and functions (spec 7.1.4)
Both are defined with def. A function returns a value with return; a procedure carries out a task (often printing) and returns nothing (Python gives it the value None).
def show_banner(title):
print("*" * len(title))
print(title)
def area(width, height):
return width * height
show_banner("Report")
a = area(3, 4)
print(a + area(2, 5))Output:
******
Report
22Parameters and arguments
A parameter is the name in the def line (width, height). An argument is the value passed in the call (3, 4). Arguments are matched to parameters by position, or by name: area(height=4, width=3). A parameter can have a default value: def greet(name, greeting="Hello").
return ends the function
As soon as return runs, the function stops and sends the value back. A function can return several values as a tuple.
def min_max(values):
low = values[0]
high = values[0]
for v in values:
if v < low:
low = v
if v > high:
high = v
return low, high
smallest, largest = min_max([7, 2, 9, 4])
print(smallest, largest)Output:
2 9Local variables
Parameters and variables assigned inside a subprogram are local: they exist only while it runs and cannot be seen outside. Pass data in through parameters and get results out with return, rather than using global variables. This is covered further in the program design lesson.
Worked example
Write a function is_leap(year) that returns True for leap years: divisible by 4, except century years, which must be divisible by 400. Test it on 2024, 1900 and 2000.
- def is_leap(year):
- return (year % 4 == 0 and year % 100 != 0) or year % 400 == 0
- Results: True False True
Answer: True, False, True
Check yourself
- What is displayed when this code runs?
```python
def triple(n):
return n * 3
print(triple(4) + triple(2))
```
Show answer
18. triple(4) = 12 and triple(2) = 6, so 18.
- What is displayed when this code runs?
```python
def check(x):
if x > 10:
return "big"
return "small"
print("never")
print(check(15), check(3))
```
Show answer
big small. return ends the function, so the print inside is never reached: big small.
- What is displayed when this code runs? (Output lines are separated by / in the options.)
```python
def greet(name):
print("Hi", name)
result = greet("Sam")
print(result)
```
- Hi Sam / Hi Sam
- Hi Sam / None
- Hi Sam
- Sam / None
Show answer
Hi Sam / None. greet is a procedure: it prints 'Hi Sam' and returns nothing, so result is None: Hi Sam, then None.
- What is displayed when this code runs?
```python
def power(base, exp=2):
return base ** exp
print(power(5), power(2, 5))
```
Show answer
25 32. power(5) uses the default exp = 2, giving 25; power(2, 5) gives 32: 25 32.
- What is displayed when this code runs?
```python
def change(n):
n = n + 10
return n
x = 5
y = change(x)
print(x, y)
```
Show answer
5 15. n is a local copy of the value 5; changing it does not change x. The function returns 15: 5 15.
- In def total(price, qty): and the call total(4.5, 3), what are 4.5 and 3?
- Parameters
- Local constants
- Arguments
- Return values
Show answer
Arguments. Values passed in a call are arguments; price and qty are the parameters.
- This function should return the average of a list. Which line completes it?
```python
def average(values):
total = 0
for v in values:
total = total + v
__________
print(average([4, 6, 11]))
```
- `return total / len(values)`
- `print(total / len(values))`
- `return total / len(v)`
- `return total // len(values)`
Show answer
`return total / len(values)`. The function must return the total divided by the number of values: 21 / 3 = 7.0.
- This code should display 16 but it does not. Which line number contains the error?
```
1 def rectangle_perimeter(length, width):
2 return 2 * (length + length)
3
4 print(rectangle_perimeter(5, 3))
```
Show answer
2. Line 2 uses length twice and ignores the width parameter. The fix is return 2 * (length + width).
Sources
- Pearson Edexcel International Advanced Subsidiary/Advanced Level in Computer Science specification (XCP01/YCP01), first teaching 2026
- Pearson IAL Computer Science Unit 2 Programming Language Subset (PLS), Python 3
- Pearson IAL Computer Science IAS sample assessment materials (Unit 2 paper and mark scheme)
Last updated 8 November 2026. Spotted a mistake? Tell us.