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?

    csp-6-4-1: What is the side length for a square drawn by the following procedure?

    def square(turtle):
        size = 200
        turtle.forward(size)
        turtle.right(90)
        turtle.forward(size)
        turtle.right(90)
        turtle.forward(size)
        turtle.right(90)
        turtle.forward(size)
        turtle.right(90)
    
  • 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.

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).

    csp-6-4-2: What shape would the following code draw?

    def mystery(turtle, size):
        turtle.forward(size)
        turtle.right(90)
        turtle.forward(size / 2)
        turtle.right(90)
        turtle.forward(size)
        turtle.right(90)
        turtle.forward(size / 2)
        turtle.right(90)
    
    from turtle import *  # use the turtle library
    space = Screen()      # create a turtle screen (space)
    malik = Turtle()      # create a turtle named malik
    mystery(malik, 100)   # draw something with size = 100
    
  • 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.

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.

    csp-6-4-3: In the following code what are the arguments (actual parameters)?

    def square(turtle, size):
        turtle.forward(size)
        turtle.right(90)
        turtle.forward(size)
        turtle.right(90)
        turtle.forward(size)
        turtle.right(90)
        turtle.forward(size)
        turtle.right(90)
    
    from turtle import *       # use the turtle library
    space = Screen()           # create a turtle screen (space)
    imani = Turtle()           # create a turtle named imani
    square(imani, 25)      # draw a square with size 25
    
  • 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-4-5: csp-6-4-4: 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,  but the lines are mixed up.  Drag the lines into
the correct order on the right.from turtle import *
---
space = Screen()
---
imani = Turtle()
---
square(imani, 75)
---
imani.forward(100)
---
square(imani, 50)
        
Next Section - Naming Sets of Procedures and Functions