Naming Input¶
What if we want to draw a different size square, like one with a side length of 50? We could change each of the calls to the forward
procedure as shown below.
But, this means we have to change each of the four forward
statements and we could make a mistake and not set all of them to the same number. Is there a better way? What if we create a variable size
and set its value to the amount to move forward?
- 100
- How much will it go forward?
- 50
- What value is size set to?
- 200
- Size is set to 200 in line 2 so this will draw a square that has a side length of 200.
- 90
- It turns 90 degrees. It doesn't go forward 90.
csp-6-6-3: What is the side length for a square drawn by the following procedure?
1def square(turtle): 2 size = 200 3 turtle.forward(size) 4 turtle.right(90) 5 turtle.forward(size) 6 turtle.right(90) 7 turtle.forward(size) 8 turtle.right(90) 9 turtle.forward(size) 10 turtle.right(90)
Now the program is easier to change since we only have one line to change size = 50
to draw another size square. But, we still have to change the program in order to draw a different size square. Is there a better way?
We can add an additional input to the function that specifies the size of the square. Just separate the names for the inputs with a comma: (turtle,size)
as shown below and be sure to specify the actual size when you call the procedure square(malik, 100)
or square(malik, 50)
.
- square
- Check the 2nd and 4th forwards. How much do they move forward by?
- rectangle
- This will draw a rectangle with two sides with the specified size and two sides half that size. Copy this code into the area above and run it.
- triangle
- A triangle has 3 sides.
csp-6-6-5: What shape would the following code draw?
1def mystery(turtle,size):
2 turtle.forward(size)
3 turtle.right(90)
4 turtle.forward(size / 2)
5 turtle.right(90)
6 turtle.forward(size)
7 turtle.right(90)
8 turtle.forward(size / 2)
9 turtle.right(90)
10
11from turtle import * # use the turtle library
12space = Screen() # create a turtle screen (space)
13malik = Turtle() # create a turtle named malik
14mystery(malik, 100) # draw something with size = 100
The inputs that are specified in a function or procedure definition are also called parameters or formal parameters. So turtle
and size
are both parameters (formal parameters) in the square
procedure. Notice that when we call square
we have to specify the actual values for the inputs. The actual values passed into the function as inputs are called the arguments or actual parameters. In the call square(malik, 50)
both malik
and 50
are arguments (actual parameters) to the square
procedure.
- turtle and size
- These are the names of the parameters (formal parameters).
- malik and 25
- Look again at the code above. Is that the name of this turtle?
- imani and 25
- The turtle is named imani and the size is 25 in the code: square(imani, 25).
csp-6-6-6: In the following code what are the arguments (actual parameters)?
1def square(turtle,size):
2 turtle.forward(size)
3 turtle.right(90)
4 turtle.forward(size)
5 turtle.right(90)
6 turtle.forward(size)
7 turtle.right(90)
8 turtle.forward(size)
9 turtle.right(90)
10
11from turtle import * # use the turtle library
12space = Screen() # create a turtle screen (space)
13imani = Turtle() # create a turtle named imani
14square(imani, 25) # draw a square with size 25
The following code assumes that a procedure square has been defined that takes a size. The code should create a turtle and then use it to draw a square, move forward, and draw a second square as shown at left, <img src=”../_static/SquareForwardSquare.png” width=”150” align=”left” hspace=”10” vspace=”5”/> but the lines are mixed up. Drag the lines into the correct order on the right.