STARLEAP / PYTHON TURTLE
Build 2026.09.17-001210-0400
On this page
- Start With A Turtle File
- Step 1 - Define And Call
draw_square() - Step 2 - Prove The Call Matters
- Step 3 - Add
draw_triangle() - Step 4 - Add One More Shape
- Step 5 - Notice The Pattern
- Step 6 - Arguments Are Values You Hand In
- Step 7 - Rename The Arguments
- Step 8 - Call One Function Many Ways
- Step 9 - Move With
t.goto(x, y) - Final Project - Fill A Page With Polygons
- Teacher Check
4. Turtle Functions: Name A Chunk
You already know variables can store values and loops can repeat a chunk. Today you will give a chunk of code a name.
A function is a named chunk of code. def defines it. Calling the function runs it.
Today you will start with shape-specific functions like draw_square() and draw_triangle(). Then you will make one flexible function that can draw many different regular polygons.
Start With A Turtle File
Save a new file named turtle_functions.py.
Start with this template.
import turtle
screen = turtle.Screen()
screen.setup(600, 800, 0, 0)
t = turtle.Turtle()
t.speed(0)
# Your function code goes here.
screen.mainloop()
Run the program. A blank turtle window is fine. Close the turtle window before you change the code again.
Step 1 - Define And Call draw_square()
Replace the middle comment with this code.
t.speed(0)
def draw_square():
for i in range(4):
t.forward(80)
t.left(90)
draw_square()
screen.mainloop()
Run the program.
The def draw_square(): line defines the function. The indented lines belong to it. The draw_square() line calls it.
If nothing happened on your screen, check the call line. def draw_square(): teaches Python the chunk, but draw_square() actually runs it.
Step 2 - Prove The Call Matters
Change only the call at the bottom, then run the program.
def draw_square():
...
draw_square()
t.penup()
t.forward(120)
t.pendown()
draw_square()
screen.mainloop()
Step 3 - Add draw_triangle()
Add a second named chunk above your calls. Then call it after moving the turtle forward.
def draw_square():
...
def draw_triangle():
for i in range(3):
t.forward(90)
t.left(120)
draw_square()
t.penup()
t.forward(130)
t.pendown()
draw_triangle()
screen.mainloop()
Run the program.
Now your program has two function definitions and two function calls.
Step 4 - Add One More Shape
Add a hexagon function above your calls. Then call it after the triangle.
def draw_triangle():
...
def draw_hexagon():
for i in range(6):
t.forward(55)
t.left(60)
draw_triangle()
t.penup()
t.forward(130)
t.pendown()
draw_hexagon()
screen.mainloop()
Run the program.
You now have draw_square(), draw_triangle(), and draw_hexagon(). Each function has a clear name, and each call tells Python which chunk to run.
Step 5 - Notice The Pattern
These functions are useful, but they repeat the same idea.
def draw_square():
for i in range(4):
t.forward(80)
t.left(90)
def draw_triangle():
for i in range(3):
t.forward(90)
t.left(120)
def draw_hexagon():
for i in range(6):
t.forward(55)
t.left(60)
Each function chooses a number of sides, a side length, and a turn angle. The next version will let one function receive those values.
Step 6 - Arguments Are Values You Hand In
Python lets a function accept values when you call it. Those values are called arguments.
This version works, but the argument names are not helpful.
t.speed(0)
def draw_polygon(a, b):
angle = 360 / a
for i in range(a):
t.forward(b)
t.left(angle)
draw_polygon(5, 70)
screen.mainloop()
Run the program.
The computer can use a and b, but readers have to stop and ask, "What does a mean? What does b mean?" Short names are not always better names.
Step 7 - Rename The Arguments
Replace a and b with names that explain their jobs.
t.speed(0)
def draw_polygon(sides, length):
angle = 360 / sides
print(angle)
for i in range(sides):
t.forward(length)
t.left(angle)
draw_polygon(5, 70)
screen.mainloop()
Run the program.
Now the code says what each argument means: the first value becomes sides, and the second becomes length.
Step 8 - Call One Function Many Ways
Change only the call line each time.
def draw_polygon(sides, length):
...
draw_polygon(3, 80)
screen.mainloop()
Run the program. Then try these calls one at a time.
draw_polygon(4, 70)
draw_polygon(5, 60)
draw_polygon(6, 50)
draw_polygon(8, 35)
One function can draw many shapes because each call hands in different arguments.
Step 9 - Move With t.goto(x, y)
So far, the turtle has moved forward from wherever it already was. t.goto(x, y) teaches the turtle to jump to an exact spot on the screen.
The center of the turtle window is (0, 0). Bigger x values move right. Smaller or negative x values move left. Bigger y values move up. Smaller or negative y values move down.
Use t.penup() before t.goto(...) and t.pendown() after it when you want the turtle to move without drawing a travel line.
def draw_polygon(sides, length):
...
t.penup()
t.goto(-200, 120)
t.pendown()
draw_polygon(5, 60)
screen.mainloop()
Run the program.
Change only the two numbers in t.goto(-200, 120), then run again. Notice where the shape starts.
Final Project - Fill A Page With Polygons
Use this full program to make a page of different regular polygons.
import turtle
screen = turtle.Screen()
screen.setup(600, 800, 0, 0)
t = turtle.Turtle()
t.speed(0)
def draw_polygon(sides, length):
angle = 360 / sides
print(angle)
for i in range(sides):
t.forward(length)
t.left(angle)
def draw_at(x_position, y_position, sides, length):
t.penup()
t.goto(x_position, y_position)
t.pendown()
draw_polygon(sides, length)
draw_at(-230, 150, 3, 75)
draw_at(-60, 150, 4, 70)
draw_at(120, 150, 5, 60)
draw_at(-230, -80, 6, 55)
draw_at(-60, -80, 7, 45)
draw_at(120, -80, 8, 40)
screen.mainloop()
Run the program.
The draw_at(...) function has four argument names. x_position and y_position say where the turtle should go. sides and length say what shape to draw after the turtle gets there.
Make the page yours by changing the argument values in the calls. Keep the function names and argument names clear enough that someone else can read what your program is doing.
Teacher Check
Show your page of polygons. Then point to:
- one function definition
- one function call
- the two argument names in
draw_polygon(sides, length) - the
t.goto(x_position, y_position)line - two different calls that hand in different argument values
Say this out loud: "A function is a named chunk of code. Arguments are values I hand to the function when I call it. Meaningful argument names make code easier to read later."