146 Chapter 7
Parameters: Feeding Your Function
When creating a function, we can define parameters for that
function. Parameters allow us to send information to the func-
tion by passing values to it as arguments inside its parentheses.
We’ve been passing arguments to functions since our first print()
statement. When we code print("Hello"), "Hello" is an argument
representing the string value that we want printed to the screen.
When we call the turtle function t.left(90), we’re passing the value
90 as the number of degrees we want our turtle to turn left.
The random_spiral() function didn’t need parameters. All the
information it needed was in the code inside the function. But if we
want, functions that we build can take information in the form of
parameters. Let’s define a function, draw_smiley(), to draw a smiley
face at a random location on the screen. This function will take
a pair of random coordinates and draw the smiley face at those
coordinates. We’ll define and call draw_smiley() in a program called
RandomSmileys.py. The complete program is shown on page 151—
but let’s build it step by step.
Smileys at Random Locations
We want to write a program that, rather than drawing random
spirals, draws smiley faces. It’ll take a bit more planning to draw
a smiley face than it did to randomly pick a color and size and
draw a spiral. Let’s go back to our friend from Chapter 6, a piece
of graph paper. Because we haven’t drawn something as compli-
cated as a smiley face in our programs before, it’s best to draw
this on paper first and then trans-
late it into code, one part at a time.
Figure 7-1 shows a smiley face on a
graph-paper grid that we can use to
plan our drawing.
Our program will draw smileys
like this one all over the screen at
random (x, y) coordinates. The func-
tion definition for draw_smiley() will
take two parameters, x and y, for the
location where the smiley is to be
drawn. As shown in Figure 7-1, we
will draw the smiley face as if it were
0
−10−20−30−40−50 01020304050
10
20
30
40
50
60
70
80
90
100
Figure 7-1: We’re planning
our program by drawing a
smiley face on graph paper
first.