Draw Turtle Shapes With Functions

Draw Turtle Shapes With Functions

Functions in Python are a convenient way to package up several lines of code into one simple keyword that can be called. When the keyword is called, all of the code contained in the function runs. In the tutorial covering loops and Turtle, we used a loop to draw a square. Now, we can put all of that logic into a simple draw_square() function, and anytime we want to draw a square we simply call the draw_square() function. Let’s build that function now.


Define A Function

To create your own function you begin by giving it a name. The name can be anything you want, so long as it’s not already a Python keyword. The name of the function should be descriptive of what the function does. This way you can remember what they’re for when you want to call them. Let’s call our function draw_square() because that is exactly what it does.

from turtle import *

drawing_area = Screen()
drawing_area.setup(width=750, height=500)
shape('arrow')


def draw_square():
    for i in range(4):
        forward(100)
        right(90)

To call the function, all you need to do is write the name of the function followed by parentheses() like so.

from turtle import *

drawing_area = Screen()
drawing_area.setup(width=750, height=500)
shape('arrow')


def draw_square():
    for i in range(4):
        forward(100)
        right(90)


draw_square()

done()

Calling the draw_square() function will trigger the code contained inside of the function definition, causing a square to be drawn on the canvas.

draw square using function turtle


Calling Functions Many Times

Now that there is a function to draw a square, we can easily call that function again and again anytime we want to draw a square. In this program below, we use the draw_square() function in combination with some other functions built into Turtle, to draw 3 squares on the canvas all in the same program. Note that each time the draw_square() function is called, a square is drawn to the canvas.

from turtle import *

drawing_area = Screen()
drawing_area.setup(width=750, height=500)
shape('arrow')


def draw_square():
    for i in range(4):
        forward(100)
        right(90)


right(-90)
penup()
forward(50)
right(-90)
forward(200)
pendown()
draw_square()

right(180)
penup()
forward(100)
right(-90)
pendown()
draw_square()

right(90)
penup()
forward(200)
right(-90)
pendown()
draw_square()

done()

three squares in python turtle


penup() and pendown()

You may have noticed some new functions in the program above. These are the penup() and pendown() functions in Turtle. They do exactly what they sound like they would do. When you want to move the turtle without drawing a line, then you call the penup() function to virtually lift the drawing tool off of the canvas. When you want to resume drawing, you need to call the pendown() function so that the turtle leaves lines as it moves once again. Here is that same program without calling penup() or pendown(). Notice how all the squares are now connected.

move a turtle in python without drawing


An Octagon Function

Let’s create another function that draws an Octagon for us.

from turtle import *

drawing_area = Screen()
drawing_area.setup(width=750, height=500)
shape('arrow')


def draw_square():
    for i in range(4):
        forward(100)
        right(90)


def draw_octagon():
    for i in range(8):
        forward(50)
        right(45)


draw_octagon()

done()

python turtle octagon function


Calling Functions Inside Loops

By placing function calls inside of a loop, you can call a function many times to produce some interesting results. Let’s see some examples of this in action.

from turtle import *

drawing_area = Screen()
drawing_area.setup(width=750, height=500)
shape('arrow')


def draw_square():
    for i in range(4):
        forward(100)
        right(90)


def draw_octagon():
    for i in range(8):
        forward(50)
        right(45)


for i in range(20):
    draw_square()
    right(-30)
    draw_octagon()

done()

python turtle functions in loops