# 5. Procedures

We have done some python, now for procedures

## 5.1. Big Question

- What are procedures?
- How can I use procedures to make my code easier to read?
- How can I use procedures to make my code simpler?

## 5.2. Key words

- Procedure
- Indentation
- `def`

## 5.3. Procedures (Task 1)

Read the text below, then discuss with a partner, then click the correct answers.

> A procedure is a set of instructions with a name. Like turn on the light, or turtle.forward.

> Some times we want to make our own procedures. May be to draw a square or triangle. After defining a new procedure to draw a square we can just say `draw_square()`, or `draw_square(100)`, in the 2nd example the 100 could tell it how big to draw the square.

Procedures-1.1: Why would you use a procedure (def), in your code?
- It allows code reuse.
- Yes, this is the main reason to use procedures.
- It will make it easier to change the code later.
- Yes, because there is less code, it will be easier to change.
- To make a decision.
- No, you would use a selection (if), to do that (we have not done them yet).
- I will not have to type as much.
- Yes, because there is less code, you will not have to type as much.
- To calculate the value of an angle.
- No, it is not used to calculate.

## 5.4. Procedures (Task 2)

Procedures-1.2: What does the program do when it finishes defining a procedure (def)?
- The program stops.
- No, the program stops when there is no more code to run.
- It runs the line of code after the definition.
- Yes, it does what ever is next.
- It starts the program again.
- No, it would only start again, if you tell it to.
- It runs the procedure.
- No, a def only defines a procedure, it does not yet run it.

## 5.5. Procedures (Task 3)

Let us now look at a program. Looks like some sort of poem. The first version is in scratch.

Then this version is in a sort of pythony scratch. It looks like scratch, but is python.

Now in python. Run the program one line at a time, by pressing forward. Don’t forget to predict what it will do, before each time you press forward. Remember I don’t expect you to get it first time.

(procedures_2_3)

#### Check your understanding

What line will be executed after the current line executes?

## 5.6. Procedures (Task 4)

Procedures-1.4: How many times did we define the procedure verse4?
- 1.
- Yes, we defined it one, and used it twice
- 2.
- No, we used it twice, but how many times did we define it.
- 3.
- No, how many times do you see

      def verse4():

- 4.
- No, how many times do you see

      def verse4():

## 5.7. Procedures (Task 5)

            Procedures-1.5: Drag the blocks to the right, to make a program that draws a square, turns, and draws another square.import turtle
    ---
    def draw_square():
    ---
        for i in range (4):
    ---
            turtle.forward(50)
            turtle.left(90)
    ---
    draw_square()
    turtle.left(45)
    ---
    draw_square()

## 5.8. Procedures (Task 6)

Procedures-1.6: In the above code, two of the lines are indented twice. Why?
- Because they are turtle commands.
- There is another turtle command that is not indented, so it can not be for this reason.
- Because these are the commands in the procedure definition (def).
- They are in a procedure definition, so this explains one level of indentation.
- Because these are the commands to be repeated.
- They are in a loop, so this explains one level of indentation.
- Because they are in the loop that is in the procedure definition.
- Yes, we can put things in things in things.

## 5.9. Procedures (Task 7)

Procedures-2.7 Drag the sentence endings to the sentence starts on the right. Have you done the ones that you know? Have you discussed with another pair?
- indented.
- The code belonging to the defined procedure is
- Remembers the indented code, and gives it the name that follows def, but does not run the code.
- When the python interpreter gets to a line starting def it
- ends with a colon:
- The line

      def … ()

- the space at the beginning of the line.
- Indentation is

## 5.10. Procedures (Task 8)

            Procedures-1.8: Rearrange to make a program that draws a triangle.import turtle
    ---
    def draw_triangle():
    ---
        for i in range (3):
    ---
            turtle.forward(50)
            turtle.left(120)
    ---
    draw_triangle()

## 5.11. Procedures (Task 9)

            Procedures-1.9: Rearrange to make a program that draws a triangle, then a square.import turtle
    def draw_triangle():
    ---
       for i in range (3):
    ---
          turtle.forward(50)
          turtle.left(120)
    ---
    def draw_square():
    ---
       for i in range (4):
    ---
          turtle.forward(50)
          turtle.left(90)
    ---
    draw_square()
    turtle.left(90)
    turtle.forward(50)
    turtle.right(90)
    draw_triangle()

## 5.12. Procedures (Task 10)

Now click Reveal code, and try it out.

import turtle def draw_triangle(): for i in range (3): turtle.forward(50) turtle.left(120) def draw_square(): for i in range (4): turtle.forward(50) turtle.left(90) draw_square() turtle.left(90) turtle.forward(50) turtle.right(90) draw_triangle()

## 5.13. Procedures (Task 11)

            Procedures-1.10: Rearrange to make a program that draws 4 trianglesimport turtle
    ---
    def draw_triangle():
    ---
       for i in range (3):
    ---
          turtle.forward(50)
          turtle.left(120)

    ---
    for i in range(4):
    ---
       draw_triangle()
       turtle.left(90)

## 5.14. Procedures extra

Use the following area to experiment.

import turtle
Next Section - 6. Arithmetic