Parameters and return values
Information in and an answer out; procedures and functions.
Covers OCR J277 2.2.3 Additional programming techniques · AQA 8525 3.2.10 Structured programming and subroutines (procedures and functions) · Edexcel 1CP2 6.6.2 Writing functions and procedures
Do this lesson in the simulatorPrint it: worksheet (PDF) · answers (PDF) · as a web page
The functions in the last lesson did the same thing every time. Real functions take information in and give an answer back, the way forward(50) takes a speed and distance() gives a number. This lesson is about both directions.
Parameters: information in
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
def square(size):
for i in range(4):
forward(60, distance=size)
turn_right(30, angle=90)
square(15)
square(30)
size is a parameter: a variable in the function's definition that gets its value from the call. The value in the call, 15 or 30, is the argument. For square(15), size is 15 inside the body; for square(30) it is 30. One function, any size of square.
Several parameters
Parameters are separated by commas, and the arguments are matched to them in order:
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
def polygon(sides, length):
print("polygon with", sides, "sides")
for i in range(sides):
forward(60, distance=length)
turn_right(30, angle=360 / sides)
polygon(3, 20)
polygon(6, 12)
polygon(3, 20) puts 3 in sides and 20 in length. Swap them, polygon(20, 3), and you get a 20-sided shape with tiny sides: the order matters.
return: an answer out
A function can hand a value back to the line that called it, with return:
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
def area(width, height):
return width * height
def is_clear(cm):
return distance() > cm
print("a 20 by 30 rectangle covers", area(20, 30), "square cm")
big = area(40, 40)
print("and a 40 cm square", big)
if is_clear(40):
print("clear for 40 cm, going")
forward(50, distance=20)
else:
print("not clear")
return does two things: it ends the function straight away, and it makes the call stand for the value. area(20, 30) becomes 600 wherever it is written, in a print, in an assignment, or in an if.
Printing is not returning
This is the most common mix-up with functions:
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
def double_print(n):
print(n * 2)
def double_return(n):
return n * 2
a = double_print(5)
b = double_return(5)
print("a is", a)
print("b is", b)
The first line, 10, comes from double_print: it shows the answer but hands nothing back, so a is None, Python's word for no value. double_return shows nothing itself, but it gives the program a 10 to keep in b. Print when a person needs to see something; return when the program needs to use it.
Procedures and functions
A subprogram that returns a value, like area or distance(), is a function. One that only does something, like square or led("red"), is a procedure. Python uses def for both; the exam languages write them differently.
Task: any polygon
Write polygon(sides, length) that prints polygon with <sides> sides and drives the shape. Call it for a pentagon (5 sides, 20 cm) and then a triangle (3 sides, 20 cm). Finish facing roughly the way you started.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
def polygon(sides, length):
print("polygon with", sides, "sides")
# drive it
polygon(5, 20)
Task: room ahead
Write a function room_ahead(margin) that returns how far the robot can drive before it is margin cm from the wall: the distance ahead minus the margin. Then use it to drive up to 15 cm from the wall in one move, and print stopped with <cm> cm to spare, using distance() again at the end.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
def room_ahead(margin):
print(distance() - margin)
forward(50, distance=room_ahead(15))
Challenges
- Write
average(a, b, c)that returns the average of three numbers, and test it with values you can check. - Give
polygona third parameter for the speed. - Write
creep(limit)that drives in 5 cm steps until the wall is underlimitcm away, and returns how many steps it took.
Past paper questions on this
Real exam questions that test this, each worked through with a program you can run.
- AQA 8525 June 2025 Paper 1, Question 7: Read a subroutine: purpose, outputs, return value, parameter 7 marks
- AQA 8525 June 2025 Paper 1, Question 11: Trace findLetter, then write the word game that calls it 14 marks
- AQA 8525 June 2024 Paper 1, Question 10: Trace three subroutines that call each other 2 marks
- AQA 8525 June 2024 Paper 1, Question 12.1 to 12.5: A sliding puzzle: move tiles, and read nested loops that find the blank 7 marks
- AQA 8525 June 2024 Paper 1, Question 12.6, 12.7: Use given subroutines to check a row and to play until solved 10 marks
- AQA 8525 June 2023 Paper 1, Question 4: Trace a Python function with three sets of inputs, then make it robust 4 marks
- AQA 8525 June 2023 Paper 1, Question 12.4: Fix a subroutine's parameter and a FOR loop that counts down 3 marks
- AQA 8525 June 2023 Paper 1, Question 14: Fill the gaps in an algorithm that uses a two-dimensional array 3 marks