Skip to content

Function Parameters: Drawing Squares Anywhere

By the end of this lesson you'll be able to:

  • Add parameters to a function so callers can pass in values
  • Call the same function multiple times with different colors and positions
  • Use penup() and goto() to reposition the turtle without drawing

One function can do many different things if you give it parameters — inputs it receives from the caller. In this lab, you'll write a square() function that accepts a color and a position, then call it four times to paint four different-colored squares around the canvas.

Welcome to This Lesson!

Monty waving welcome You already know how to define a function. Today you'll make functions that can take instructions! A function with parameters is like a vending machine — you press a button (pass in a value) and get exactly what you asked for. Let's code it together!

Parameters: Inputs for Functions

A parameter is a variable inside a function's parentheses that receives a value when the function is called:

1
2
3
4
def square(my_color, x, y):
    # my_color, x, and y are filled in by the caller
    monty.color(my_color)
    ...

When you call square('red', -50, 80), Python sets my_color = 'red', x = -50, and y = 80 automatically — no extra assignment needed.

Sample Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import turtle
monty = turtle.Turtle()
monty.shape('turtle')

size = 60
angle = 90

def square(my_color, x, y):
    monty.penup()
    monty.goto(x, y)
    monty.pendown()
    monty.color(my_color)
    monty.begin_fill()
    for i in range(4):
        monty.forward(size)
        monty.right(angle)
    monty.end_fill()

square('red',    -80,  80)
square('orange',  20,  80)
square('green',  -80, -20)
square('blue',    20, -20)
monty.hideturtle()

What Do You Think Will Happen?

Monty thinking square() is called four times with four different colors and four different positions. How many squares will appear on the canvas, and where will they be? Make your guess — then click Run!

Try It Now

Edit the code below and click Run to see the result right on this page. No account needed — everything runs in your browser.


  

Were you right? Four filled squares in a 2×2 grid — red and orange on top, green and blue on the bottom.

How It Works

penup() lifts the pen so no line appears while the turtle travels to the new position. goto(x, y) moves the turtle to that exact coordinate. pendown() lowers the pen so drawing resumes. This penup/goto/pendown pattern is how you start any shape at a specific location.

Each time square() is called with different arguments, Python creates fresh copies of my_color, x, and y inside the function — the previous call's values are gone. The function uses them, finishes, and waits to be called again.

Explanation Table

Line What it does
def square(my_color, x, y): Defines square with 3 parameters: color, horizontal position, vertical position
monty.penup() Lifts the pen — no line drawn while moving
monty.goto(x, y) Moves the turtle to coordinates (x, y)
monty.pendown() Lowers the pen — drawing resumes
square('red', -80, 80) Calls the function: my_color='red', x=-80, y=80

The Canvas Grid

Monty with a tip The canvas center is (0, 0). Positive x goes right, negative goes left. Positive y goes up, negative goes down. So (-80, 80) is upper-left and (20, -20) is lower-right.

Learning Check

Your Turn — Add a Fifth Square

Monty thinking The code below draws four squares using the square() function. Add one line at the bottom to draw a fifth purple square centered at (-30, 30).


  

Experiments

Try these changes to the four-squares code. Predict first, then run!

  1. Change size = 60 to size = 40. You'll know it worked when all four squares are smaller.

  2. Add a fourth parameter my_size to the function definition, and use it in forward(my_size) instead of forward(size). Then pass different sizes in each call. You'll know it worked when the four squares are all different sizes.

  3. Add monty.pensize(3) inside the function, before the loop. You'll know it worked when all four squares have a thicker outline.

  4. Change the range(4) inside the function to range(3) and angle to 120. You'll know it worked when you see four filled triangles instead of squares.

  5. Use a for loop to call square() six times with colors from a list. You'll know it worked when six shapes appear across the canvas.

Great Work!

Monty celebrating Parameters make functions flexible — write once, use everywhere with different values. In the next lab you'll meet the random library and let Python choose colors and positions for you! Let's code it together!