Parameters and return values

Information in and an answer out; procedures and functions.

F4.2Functions and structured codeGCSE15 min

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.

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.

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

Both show 10 on the screen, but only double_return gives the program a value to keep. double_print returns nothing, which Python writes as None. 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

  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.