Functions and Flowers¶
By the end of this lesson you'll be able to:
- Define your own function with
defand call it by name - Call a function multiple times from inside a loop
- Build a flower drawing by repeating a
petal()function
What if you could give a chunk of code a name and run it whenever you want — just by saying the name? That's what a function is. In this lab you'll define a petal() function and call it in a loop to grow a full flower on the canvas.
Welcome to This Lesson!
Functions are the secret to writing code that's short, clear, and easy to reuse.
Today we're going to grow a flower together — one petal at a time!
Let's code it together!
Defining a Function¶
A function is a block of code you write once and can run as many times as you want by calling its name. You create one with def:
1 2 3 4 5 6 7 | |
This function draws one triangle — one petal. To use it, call petal(). To draw four petals, call it four times (or put it in a loop).
Scratch Bridge
In Scratch you used "My Blocks" to define a custom block and then drag it where you needed it.
In Python, def petal(): defines the block, and petal() is how you use it — every time you call it, the whole function runs!
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 24 | |
What Do You Think Will Happen?
petal() is called 4 times, and each petal rotates 90 degrees from the last.
What do you think the full drawing will look like — a flower, a pinwheel, something else?
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 pink triangles arranged around a center with a green stem — a flower!
How It Works¶
def petal(): creates the function but doesn't run it yet. The code inside it stays idle until you call petal(). When the for i in range(4): loop runs, it calls petal() four times. Each call draws one triangle and then rotates the turtle 90 degrees so the next petal points in a new direction.
begin_fill() and end_fill() paint the inside of whatever shape the turtle traces between those two calls. hideturtle() removes the turtle cursor at the end for a cleaner result.
Explanation Table¶
| Line | What it does |
|---|---|
def petal(): |
Defines a function called petal — code here runs when petal() is called |
monty.begin_fill() |
Starts recording the path for filling |
monty.color('pink') |
Sets the fill and outline color to pink |
for i in range(3): |
Loops 3 times inside the function to draw each triangle side |
monty.end_fill() |
Fills the enclosed area with the current color |
monty.right(90) |
Rotates 90° so the next petal faces a new direction |
for i in range(4): petal() |
Calls the function 4 times to draw 4 petals |
Learning Check¶
Spot the Bug!
The program below tries to draw 4 petals, but only one appears before the program stops.
The function is never called inside the loop. Can you find and fix the problem?
Experiments¶
Try these changes to the flower code. Predict first, then run!
-
Change
monty.color('pink')tomonty.color('yellow'). You'll know it worked when the petals are yellow instead of pink. -
Change
for i in range(4): petal()tofor i in range(6): petal()and the rotation insidepetaltomonty.right(60). You'll know it worked when you see 6 petals instead of 4. -
Change
distance = 80todistance = 120. You'll know it worked when the petals are noticeably larger. -
Add an
if/elseinsidepetal()so it alternates between'pink'and'purple'usingi % 2. You'll know it worked when the petals alternate colors. (Hint: you'll need to passito the function.) -
Change the stem direction: replace
monty.right(45)withmonty.left(45). You'll know it worked when the stem grows upward-left instead of downward-right.
Great Work!
You defined your first function and used it to build something beautiful!
In the next lab you'll give your functions parameters so the same function can draw
different things depending on what you pass in. Let's code it together!