← Python functions and events

Functions that give answers

Return a value, combine functions, and use them inside loops.

Calculators, maps and games are full of functions that take something in and give an answer back.

You can write functions that draw. Now you will write functions that give an answer back, and combine several functions to build something bigger.

PredictRead the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.

Predict

returns.py
from turtle import *

scale = 20


def metres(m):
    return m * scale


forward(metres(5))
right(90)
forward(metres(3))

metres(5) gives back a number. What do you think forward(metres(5)) does? How far will the turtle move, and how far does the second forward take it?

RunRun it and compare the result with your prediction.

Run

Run it and compare it with your prediction. Try metres(10).

returns.py
from turtle import *

scale = 20


def metres(m):
    return m * scale


forward(metres(5))
right(90)
forward(metres(3))
Try it yourselfYour edits are saved in this browser.

InvestigateTrace how each line of code connects to what you see.

Investigate

print(metres(5)) shows the answer under the drawing. Change scale and watch both the printed value and the drawing change.

returns.py
from turtle import *

scale = 20


def metres(m):
    return m * scale


print(metres(5))
forward(metres(5))
right(90)
forward(metres(3))

Work out the value before you run anything.

Here is a version that does not work.

What is the job of the word return?

scale is 20. What number does metres(5) give back?

This version prints 100, then stops with a TypeError on the last line. Click the line that causes it.

What does the word return do in metres?

ModifyChange the code on purpose and check the result.

Modify

room calls metres to turn sizes into pixels. Functions are working together here.

Write def area(width, depth) that returns the area.

Need a hint?

Your function should work out the answer and give it back with return, not print it.

Another hint

The area of a room is its width times its depth.

Print the area of the room that the program draws.

Need a hint?

Call your function inside print(...), for example print(area(5, 4)).

Try it yourselfYour edits are saved in this browser.

Write def area(width, depth) that returns a value.

    Run your program. This ticks when your code does all of the above.

    Print the area of the room.

      Run your program. This ticks when your code does all of the above.

      InvestigateTrace how each line of code connects to what you see.

      Mix it up

      Work this out on paper first.

      def label(n):
          if n > 50:
              return "big"
          else:
              return "small"
      
      What does label(50) give back?

      MakeBuild something new using what you have learned. Expect a few tries; that is normal.

      Make a street

      This time there is no starter. Build the street in two stages.

      Write a function that draws one house.

      Need a hint?

      Write a function that draws one house, with a size parameter.

      Use a loop to repeat it for a street of five houses.

      Need a hint?

      A loop calls your function five times. A new size for each house can come from the loop variable.

      Try it yourselfYour edits are saved in this browser.

      Write a function for one house and call it.

        Run your program. This ticks when your code does all of the above.

        Use a loop to draw five houses.

          Run your program. This ticks when your code does all of the above.

          ModifyChange the code on purpose and check the result.

          Vary a street of houses

          This is a finished street. Make the houses all the same size, then make them shrink along the street. Add a sixth house, or change the gap between them.

          Try it yourselfYour edits are saved in this browser.
          For teachers

          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. About 55 minutes.

          Open the teacher guide: answers, solutions, slides and a worksheet.

          Students will be able to

          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

          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.

          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.

          What each step is for

          Now you can
          Next lesson5. Functions that decidePut an if inside a function so it chooses its answer, then call it for every item in a list: decisions, lists and functions working together.