Naming Sets of Procedures and Functions

So far we’ve seen names for values, like variables that hold strings and numbers. We’ve also seen how to name (define) functions or procedures and use additional names to store the inputs to those functions or procedures.

Sometimes, you will want to have have a whole group of functions and/or procedures, and you will want to store them somewhere and name that whole set of functions and procedures. In fact, you can. And in fact, you have already used this ability.

That is what you are doing when you execute a statement like from turtle import *. That is where the procedures like forward and right and functions like Screen are defined. We can mix procedures and functions that we define with procedures and functions that we import.

 
1
def 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
11
from turtle import *   # use the turtle library
12
space = Screen()       # create a turtle screen (space)
13
emily = Turtle()       # create a turtle named emily
14
emily.setheading(90)   # Point due north
15
emily.forward(10)      # Offset the shapes a bit
16
emily.right(18)        # And turn each one a bit
17
square(emily,100)      # draw a square with size 100
18
emily.forward(10)      # Offset the shapes a bit
19
emily.right(18)        # And turn each one a bit
20
square(emily,100)      # draw a square with size 100
21
emily.forward(10)      # Offset the shapes a bit
22
emily.right(18)        # And turn each one a bit
23
square(emily,100)      # draw a square with size 100
24

(Squares_Pattern)

csp-6-5-1: Imagine that you add one more line to the program above. Which procedure can you use safely, because it will have been defined?





Similar to the example above, make a procedure that takes in 2 parameters: a turtle and size. The procedure should draw a pentagon. Write the main code to call the pentagon function once.

3
 
1
2
3

(6_5_2_WSq)

20
 
1
# DEFINE THE PROCEDURE
2
def pentagon(turtle, size):
3
    turtle.forward(size)
4
    turtle.right(72)
5
    turtle.forward(size)
6
    turtle.right(72)
7
    turtle.forward(size)
8
    turtle.right(72)
9
    turtle.forward(size)
10
    turtle.right(72)
11
    turtle.forward(size)
12
    turtle.right(72)
13
14
# CREATE TURTLE WORLD
15
from turtle import *
16
space = Screen()
17
emily = Turtle()
18
# CALL THE PROCEDURE
19
pentagon(emily,100)
20

(6_5_2_WSa)

Next Section - Using an Image Library