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()andgoto()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!
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 | |
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 | |
What Do You Think Will Happen?
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
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
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!
-
Change
size = 60tosize = 40. You'll know it worked when all four squares are smaller. -
Add a fourth parameter
my_sizeto the function definition, and use it inforward(my_size)instead offorward(size). Then pass different sizes in each call. You'll know it worked when the four squares are all different sizes. -
Add
monty.pensize(3)inside the function, before the loop. You'll know it worked when all four squares have a thicker outline. -
Change the
range(4)inside the function torange(3)andangleto120. You'll know it worked when you see four filled triangles instead of squares. -
Use a
forloop to callsquare()six times with colors from a list. You'll know it worked when six shapes appear across the canvas.
Great Work!
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!