← Teacher guide

Functions that give answers

Students write functions that give an answer back with return, see why print is not the same, and combine small functions to build a street.

Python functions and events · Functions · about 55 minutes · Open the student lesson

Classroom resources

Load these into Waypoints Prepare: choose Load URL and paste the copied address, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.

Students will be able to

Timing

Predict and Run 10 min · Investigate 15 min · Modify 10 min · Make 15 min · Street 5 min

Before class

Read the unit primer first. The one idea to be clear on: print shows a value on the screen, but the program cannot use it; return hands the value back to the line that called the function, so it can be used, like forward(metres(5)). Try the bug task yourself: it stops with a TypeError because a function without return gives back None (Python's word for "nothing").

Running the lesson

  1. Predict: metres(5) is replaced by its answer, 100, so forward(metres(5)) is forward(100). Write this substitution on the board.
  2. Investigate: work out metres(5) before running. Then do the bug task and ask why the number is printed but the turtle still fails.
  3. Modify: area returns width * depth. Students print it to see it; the room drawing does not change.
  4. Make: there is no starter on purpose. This shows whether students can write and use a function on their own. Give the hint only after a real attempt.
  5. Street: the loop varies the size argument, so each house is different. Ask how to make them shrink instead of grow.

The hard part (on purpose)

return versus print: both show a number, but only return hands it back. Let the NoneType error happen, then ask what the function gives back.

When students get stuck

What you will seeA question to askThe fix
A TypeError mentioning NoneType, often on a forward line.Does the function return its answer, or only print it?Replace print(...) inside the function with return ..., then use the value where it is needed.
The area is worked out but nothing appears.Where is the returned value used or shown?A returned value has to be used. Print it outside the function, print(area(5, 4)).
Code after return never runs.What happens at the moment a function reaches return?return ends the function. Put any other lines before it.

Answers to the checks

  1. What comes back?: scale is 20. What number does metres(5) give back?
    100
    metres(5) returns 5 * 20 = 100, so forward(metres(5)) moves 100.
  2. Spot the bug: This version prints 100, then stops with a TypeError on the last line. Click the line that causes it.
    line 7
    print(m * scale) shows the value but does not give it back. It should be return m * scale.
  3. Explain return: What does the word return do in metres?
    It gives the answer back to the code that called the function
    return hands the value back, so forward(metres(5)) can use it. print only shows a value; it does not give it back.
  4. Mix it up: What does label(50) give back?
    small
    50 > 50 is False, because 50 is not more than 50. So the else path runs and the function returns "small".

Solutions

One possible solution for each task. Other correct programs are fine.

Modify › Write area

from turtle import *

scale = 20


def metres(m):
    return m * scale


def room(width, depth):
    for i in range(2):
        forward(metres(width))
        right(90)
        forward(metres(depth))
        right(90)


def area(width, depth):
    return width * depth


room(5, 4)

Modify › Print the area

from turtle import *

scale = 20


def metres(m):
    return m * scale


def room(width, depth):
    for i in range(2):
        forward(metres(width))
        right(90)
        forward(metres(depth))
        right(90)


def area(width, depth):
    return width * depth


room(5, 4)
print(area(5, 4))

Make a street › Draw one house

from turtle import *


def house(size):
    for i in range(4):
        forward(size)
        right(90)
    left(60)
    forward(size)
    right(120)
    forward(size)
    right(120)
    forward(size)
    right(180)


house(60)

Make a street › Draw five in a row

from turtle import *


def house(size):
    for i in range(4):
        forward(size)
        right(90)
    left(60)
    forward(size)
    right(120)
    forward(size)
    right(120)
    forward(size)
    right(180)


penup()
goto(-200, 0)
pendown()
for i in range(5):
    house(50)
    penup()
    forward(80)
    pendown()

Vary a street of houses

from turtle import *


def house(size):
    for i in range(4):
        forward(size)
        right(90)
    left(60)
    forward(size)
    right(120)
    forward(size)
    right(120)
    forward(size)
    right(180)


def move(distance):
    penup()
    forward(distance)
    pendown()


penup()
goto(-220, 0)
pendown()
for i in range(6):
    house(70 - i * 10)
    move(75)

Stretch and support

Stretch: Write a function that returns the perimeter of a room, then print the area and perimeter of three rooms.

Support: Before the Make step, write the one-house function together on the board, then let students add the loop themselves.

Exit ticket

def double(n): return n * 2. What does forward(double(30)) do?

double(30) gives back 60, so the turtle moves forward 60 steps.

What each step is for

Where it leads

Next lesson: functions that decide, an if inside a function, called for every item in a list. Then modules keep related functions where other programs can import them. Calculators, maps and games are full of functions that take something in and give an answer back. Class ideas: a class toolbox of unit conversions; a street where every house has a different design.