← Python functions and events

Functions with parameters

Make one function draw many different shapes, and keep your best ones as tools.

One well-made function can do the work of a hundred copied lines: sending a message, drawing a tile, converting a unit.

Last lesson square() always drew the same size. A parameter lets each call say what it needs.

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

params.py
from turtle import *


def square(size):
    for i in range(4):
        forward(size)
        right(90)


square(50)
square(100)
square(150)

size is a parameter. What will this draw? Where do the three squares share a corner, and why?

RunRun it and compare the result with your prediction.

Run

Run it and compare it with your prediction. Then call square with other sizes.

params.py
from turtle import *


def square(size):
    for i in range(4):
        forward(size)
        right(90)


square(50)
square(100)
square(150)
Try it yourselfYour edits are saved in this browser.

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

Investigate

polygon(sides, size) has two parameters. The angle is calculated from sides, just like in the variables lesson.

params.py
from turtle import *


def polygon(sides, size):
    angle = 360 / sides
    for i in range(sides):
        forward(size)
        right(angle)


polygon(3, 80)
polygon(4, 80)
polygon(6, 80)

What does each call pass in, and what does the function work out?

Parameters and arguments sound alike. Match them up.

Fill in what each call passes to polygon(sides, size), and the angle it calculates.

callsidessizeangle
polygon(3, 80)380
polygon(4, 80)480
polygon(6, 80)

Match each piece with its role.

sides in def polygon(sides, size):
6 in polygon(6, 80)
angle = 360 / sides
polygon(4, 80)

ModifyChange the code on purpose and check the result.

Modify

Call polygon with sides and size of your choice.

Need a hint?

Add a line like polygon(8, 50) at the bottom.

Change polygon so each call can choose its own pen colour, like polygon(3, 80, "red").

Need a hint?

A new parameter goes in the brackets of the def, and a new value goes in every call.

Another hint

Inside the function, pencolor(...) takes the value of your new parameter.

Try it yourselfYour edits are saved in this browser.

Add a fourth call to polygon and run it.

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

    Give polygon a third parameter for the pen colour.

      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 polygon(sides, size):
          for i in range(sides):
              forward(size)
              right(360 / sides)
      

      What is the turn in degrees for polygon(8, 50)?

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

      Make

      Write def pattern(sides, copies): that draws copies polygons, turning a little between them so they fan out around a point.

      Try it yourselfYour edits are saved in this browser.
      Need a hint?

      Use a loop to repeat. Inside it, call polygon, then turn a little.

      Another hint

      To go round once in total, each turn is 360 divided by the number of copies.

      Write pattern(sides, copies) and run it.

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

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

        Keep it as a button

        You have unlocked Save selection to My tools. Open Reference, put the cursor inside one of your functions, and click it. It appears under My tools, ready to copy into any program. A saved tool is the first layer of abstraction: you can use the idea again without rewriting it.

        Try it yourselfYour edits are saved in this browser.

        ModifyChange the code on purpose and check the result.

        Rearrange a floor plan

        A floor plan is room(width, depth) called many times. Run it. Move the kitchen, add a bathroom, then change scale and see the whole plan resize.

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

        Students give a function inputs (parameters), so one function can draw many sizes and shapes, then build a pattern by calling one function from another. About 55 minutes.

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

        Students will be able to

        Before class

        Run the three nested squares and try adding a colour parameter to polygon. Two words to have straight: a parameter is the name in the def (size), an argument is the value you pass when calling (50). The argument fills the parameter, in order.

        Running the lesson

        1. Predict: ask why the three squares share a corner. They all start where the turtle is, facing the same way; only size changes.
        2. Run: students try their own sizes. Ask what would happen with square(-50).
        3. Investigate: arguments go to parameters in order. polygon(6, 80) fills sides with 6 and size with 80. Swapping them gives an 80-sided shape.
        4. Modify: adding a colour parameter means changing the def and every call. That is the 'contract' between a function and its callers.
        5. Make: pattern calls polygon in a loop and turns a little each time. Set speed(0) so it draws quickly.
        6. My tools: students save a function to My tools in the Reference panel. It is a copy they can paste into later programs.

        The hard part (on purpose)

        Arguments fill parameters in order. Read the call and the def aloud side by side and match each value to its name.

        When students get stuck

        Stretch and support

        Stretch: Add a third parameter to pattern for the size, and draw a gallery of three different patterns side by side.

        Support: Give students a table of calls (square(50), square(100)) and ask them to sketch what each draws before running.

        Where it leads

        Next lesson functions give answers back with return. Parameters are how every app passes information around. Class ideas: a pattern gallery where each student chooses sides and copies; compare function designs and talk about what makes one easy to reuse.

        What each step is for

        Now you can
        Next lesson4. Functions that give answersReturn a value, combine functions, and use them inside loops.