Technovation logo

3.4. Learning to Code: Functions

If needed, you can see summaries of the turtle commands used in this section by pressing the button below.

Run the code below to see what it draws.

Before writing the code, we designed the house on a graph paper. We counted each square as 10 pixels.

Drawing of a house on graph paper

When writing the code, we added a comment at the start of the code that draws the different parts of the house—a frame, door, windows, roof, and door knob—to help us remember how the code works. Do you see how the hand-drawn design and the sections of code match up?

Q-2: Compare the code that draws the frame of the house (lines 5–16) with the code that draws the door (lines 19–30). What do you notice about these two sections of code?

(Suggestion: Drag the bottom right corner of the code editor window down farther to see more of the program at a time.)

Q-3: Compare the code that draws the top left window (lines 57–66) with the code that draws the top middle window (lines 69–78). What do you notice about these two sections of code?

Maybe you aren’t surprised that these sections of code are so similar. The house and the door are both colored rectangles, and the two windows are both colored squares, so you might think the code to draw them would be almost the same.

But this might also make you wonder: Wouldn’t code for drawing this house be much easier to write (and read) if Python had commands for drawing colored rectangles, squares and triangles?

FUNCTIONS TO THE RESCUE: You can create functions to teach the interpreter new commands!

A function is kind of like a recipe. If you write down a recipe for making jelly from “fruit,” “a sweetner,” and some sort of “clear liquid,” then you can use that recipe to create different kinds of fruit jellies. You can make cherry jelly from a bushel of cherries, refined sugar, and tap water. And you can also make blueberry jelly from a basket of blueberries, unrefined sugar, and apple juice. And so on. When you follow the recipe, you use actual berries that you bought or picked yourself where the recipe says “fruit” and the sugar you have on hand where it says “a sweetener” and tap water or other (clear) beverage where it says “clear liquid”.

To see how code can be like a recipe, run the program below. Then modify it to check out your answers to the questions that follow.

So, in a way, the code

turtle.goto(X, Y)

for i in range(2):

is like a recipe for drawing a colored rectangle, and values for the variables are like ingredients to use when following the recipe: If you indicate the ingredients to use (values for the variables) and provide a recipe (function definition), the interpreter can perform the assignments and then follow the recipe (execute code it finds in the function definition) to draw a rectangle.

The active code below contains an example of a function definition for a draw_rectangle command.

Run this program to define a draw_rectangle function.

It doesn’t look like running the program does anything. But it does! Running the program teaches the interpreter a new command.

To see this, add the following commands starting on line 20 in the code editor. Be careful not to indent either command. They both need to start in the first column. These commands are called function calls.

draw_rectangle(-150, -150, 300, 200, "blue")
draw_rectangle(-30, -150, 60, 100, "brown")

Add five additional draw_rectangle function calls to draw the windows.

Next, let’s create a draw_isosceles_triangle command for drawing the roof. We’ll start by deciding on variables to stand for the values the coder will need to supply when calling draw_isosceles_triangle. We’ll use:

The sketch below shows how the values for these variables can be used to determine the coordinates for the vertices of the triangle.


This drawing suggests the code below as a recipe for drawing an isosceles triangle. But before creating a function from the code, let’s test it.

Replace the comment at the start of this code with assignments so that the code will draw the roof for a house.

Now use this code to complete the definition for a draw_isosceles_triangle function in the program below. Then add a call to the function to draw a roof.

Complete the function definition for draw_isosceles_triangle and add a call to the function to draw the roof.

(Suggestion: To see more of the program in the editor window, enlarge it by dragging down on the bottom-right corner.)

The program should now draw everything except the door knob.

Some exercises to help solidify your understanding

Arrange the code blocks below into a definition for a function, called draw_poly, that draws a filled regular polygon. Drawing should start at the current location and use the current direction of travel of the turtle. The polygon should have N sides, each of length L, and be filled using color C.

To understand the calculation for turn_angle, see the explanation of the angles formed by a regular polygon .

Image of nested polygons created by a Turtle Graphics program

Finish the program below to draw a diagram like that shown above. Specifically, add a body for draw_poly and appropriate code after the comments.

The command can be tricky to use because the angle of the turtle affects how the circle is drawn. For an example, run the following code. In this example, all the circles are drawn starting at the origin and with the same radius; but the direction the turtle is facing is different. We use different colors to make it easier to match up the circles with the commands that draw them.

To simplify drawing circles, we can define a draw_circle function with parameters that stand for the coordinates of the center of the circle, the radius, and the pen color.

Drawing produced by a Turtle Graphics program
You have attempted of activities on this page