Parameters and return values

Information in and an answer out; procedures and functions.

F4.2Functions and structured codeGCSE15 min

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 simulator

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)

Run this in the simulator

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)

Run this in the simulator

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.

polygon(3, 20) puts 3 in sides and 20 in length and drives a triangle; polygon(20, 3) swaps them and drives a small 20-sided shapepolygon(3,20)def polygon(sides,length):3 sides of 20 cmpolygon(20,3)def polygon(sides,length):20 sides of 3 cm
The first argument always goes into the first parameter. The same two numbers in the other order draw a different shape, though both are 60 cm all the way round. The dot is where the robot starts.

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")

Run this in the simulator

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.

area(20, 30) sends 20 and 30 into width and height; return hands back 600, and the call is replaced by 600print(..., area(20, 30), ...)def area(width, height):width = 20, height = 30return width * height= 600, and the function ends20 and 30 go in600 comes outprint(..., 600, ...)the same line, as Python now sees it
return hands 600 back to the exact place the call was written, so area(20, 30) can be used anywhere a number can: in a print, stored in a variable, 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)

Run this in the simulator

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.

double_print(5) shows 10 on the screen but hands back None; double_return(5) shows nothing but hands back 10double_print(5)n = 5print(n * 2)console10hands back Noneto the programa = Nonenothing to usedouble_return(5)n = 5return n * 2consolenothinghands back 10to the programb = 10the program can use it
Two exits from a function. print goes to the console, for a person to read; return goes back to the program, which can store it. double_print uses only the first, so a is None.

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

  1. Write average(a, b, c) that returns the average of three numbers, and test it with values you can check.
  2. Give polygon a third parameter for the speed.
  3. Write creep(limit) that drives in 5 cm steps until the wall is under limit cm 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.