None in Python‑like languages).total = tax(price) + discount(price)).Is a value required by the caller?
│
├─ Yes → Write a function (include a
returnstatement)│
└─ No → Write a procedure (no
returnneeded)
Use this checklist during the Apply knowledge (AO2) part of the exam.
| Element | Description | Typical Python‑like Pseudocode |
|---|---|---|
| Name | Unique identifier (lower‑case with underscores is recommended) | def calculate_tax(...): |
| Parameters | Input values supplied by the caller; may be passed by value or by reference | (amount, rate) |
| Body | One or more statements that perform the task | Indented block under the definition |
| Return value | Only for functions; the value sent back to the caller | return tax_amount (omitted for a pure procedure) |
| Method | How It Works | Effect on Original Variable |
|---|---|---|
| Call‑by‑value | The argument’s value is copied into the parameter. | Original variable is unchanged. |
| Call‑by‑reference | The parameter becomes an alias for the argument variable. | Changes to the parameter affect the original variable. |
# Procedure that increments a number (value) and appends a tag to a list (reference)
def update_data(counter, tags):
counter = counter + 1 # call‑by‑value – does NOT change the caller's variable
tags.append('processed') # call‑by‑reference – modifies the original list
# No return statement – pure procedure
Calling code:
count = 5
my_tags = ['raw']
updatedata(count, mytags)
print(count) # → 5 (unchanged)
print(my_tags) # → ['raw', 'processed'] (modified)
def swap(a, b): # both parameters are passed by reference
temp = a
a = b
b = temp
# No return needed – the caller's variables are now swapped
Calling code (pseudo‑language that supports reference parameters):
x = 12
y = 27
swap(x, y)
print(x, y) # → 27 12
In languages that only support call‑by‑value (e.g., Python), the same effect is achieved by returning the swapped values:
def swap_vals(a, b):
return b, a
x, y = swap_vals(x, y)
def procedure_name(param1, param2, ...):
# body
statements
# (no return statement required)
def function_name(param1, param2, ...):
# body
statements
return result
procedure_name(arg1, arg2)
total = tax(price) + discount(price)
length = len(my_list) # built‑in function returns the size of the list
def print_rectangle(width, height):
for i in range(height):
print('*' * width)
print_rectangle(5, 3)
def maxofthree(a, b, c):
if a >= b and a >= c:
return a
elif b >= a and b >= c:
return b
else:
return c
largest = maxofthree(7, 12, 9)
print("Largest =", largest)
def tax(price):
return price * 0.20
def discount(price):
return price * 0.05
price = 100
total = price + tax(price) - discount(price) # total = 115.0
print("Total =", total)
log_error(message) – writes a message to a file; no value needed.calculate_tax(amount) – returns the tax amount so it can be added to a total.def areaofcircle(radius):
pi = 3.14159
return pi * radius * radius # A = πr²
r = 5
circlearea = areaof_circle(r)
print("Area =", circle_area)
def celsiustofahrenheit(c):
f = (9/5) * c + 32
print(c, "°C =", f, "°F")
celsiustofahrenheit(25)
# Inefficient – uses a loop to sum a list
def sum_list(nums):
total = 0
for i in range(len(nums)):
total = total + nums[i]
return total
# Efficient – uses built‑in function
def sum_list(nums):
return sum(nums) # O(1) in Python, highly optimised
calculate_tax).price, rate).def calculate_tax(amount: float, rate: float) -> float:
return amount * rate
For each routine write at least one test case that shows the expected output.
# Test for maxofthree
assert maxofthree(3, 9, 5) == 9
# Test for print_rectangle (visual – check output manually)
print_rectangle(4, 2)
# Expected output:
#
#
return in a function or adding one in a pure procedure – keep the distinction clear.printmaxof_three(a, b, c) that prints the greatest of three numbers (no return value).def increment(x):
x = x + 1
return x
num = 10
increment(num)
print(num)
Answer: 10 is printed because num is passed by value; the incremented value is returned but not stored.
celsiustofahrenheit(c) that returns the Fahrenheit temperature. Include the formula \(F = \frac{9}{5}C + 32\) in your answer.def duplicate_chars(text):
result = ''
for i in range(len(text)):
result = result + text[i] + text[i]
return result
Provide a more efficient version using a built‑in operation.
Your generous donation helps us continue providing free Cambridge IGCSE & A-Level resources, past papers, syllabus notes, revision questions, and high-quality online tutoring to students across Kenya.