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.
Predict: Read the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.
Run: Run it and compare the result with your prediction.
Investigate: Trace how each line of code connects to what you see.
Modify: Change the code on purpose and check the result.
Make: Build something new using what you have learned. Expect a few tries; that is normal.
Make your prediction first. Help opens once you've locked it in.
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 = 20defmetres(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 = 20defmetres(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 = 20defmetres(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.
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.
deflabel(n):
if n > 50:
return"big"else:
return"small"
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.
I can write a function that gives a value back with return.
I can explain the difference between return (hand the value back) and print (only show it).
I can build a program from several small functions working together.
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
Predict: metres(5) is replaced by its answer, 100, so forward(metres(5)) is forward(100). Write this substitution on the board.
Investigate: work out metres(5) before running. Then do the bug task and ask why the number is printed but the turtle still fails.
Modify: area returns width * depth. Students print it to see it; the room drawing does not change.
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.
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
A TypeError mentioning NoneType, often on a forward line. Ask: Does the function return its answer, or only print it? Fix: Replace print(...) inside the function with return ..., then use the value where it is needed.
The area is worked out but nothing appears. Ask: Where is the returned value used or shown? Fix: A returned value has to be used. Print it outside the function, print(area(5, 4)).
Code after return never runs. Ask: What happens at the moment a function reaches return? Fix: return ends the function. Put any other lines before it.
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
Investigate: return hands a value back so other code can use it; print only shows it. Knowing the difference is what makes functions reusable.
Modify: A function that returns a value can be combined with other code, and functions can call each other.
Mix it up: A function with an if returns one answer per call: whichever path runs decides it.
Make a street: Written without a starter, this shows whether a student can define, call and repeat a function on their own.
Now you can
Write a function that gives a value back with return.
Explain the difference between return (hand the value back) and print (only show it).
Build a program from several small functions working together.