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.
Lesson done. You've finished every step you need.
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.
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.