Year 9 Computing — Procedures, and calling them
Read a program that gives a block of lines a name, and say what runs, when, and how many times.
Name: ________________________
1.One of these four lines never runs at all. Which one?
def alpha(): print(1) def beta(): print(2) alpha() print(3)- a) The line printing 2
- b) The line printing 3
- c) The line printing 1
- d) The line calling alpha
2.A procedure whose body is a single printing line is used in five different programs. Match each way of using it to how many times its body runs.
- Defined but never called
- Called once at the end of the program
- Called inside a loop over range(1, 6)
- Called twice, and again inside a loop over range(1, 5)
- Called inside a loop over range(1, 3) that itself sits inside a loop over range(1, 5)
- 1
- 8
- 6
- 5
- 0
3.A loop does the calling here. How many numbers does it print?
def pair(): print(1) print(2) for n in range(1, 4): pair()4.Follow this code by hand. How many times does the line printing 3 run?
def step(): print(3) for n in range(1, 5): step()5.Follow this code by hand. What is the first number it prints?
def mark(): print(10) def note(): print(20) note() mark()6.The named block has a loop inside it. How many numbers does it print?
def show(): for n in range(1, 4): print(n) show() show()7.Read this program from the top down. How many numbers does it print?
def show(): print(5)- a) 0
- b) It cannot be run at all
- c) 1
- d) 5
8.A loop calls a block that calls another block. How many numbers does it print?
def inner(): print(1) def outer(): inner() inner() for n in range(1, 5): outer()
Answer key — Year 9 Computing — Procedures, and calling them
- 1. a) The line printing 2
- 2. Defined but never called → 0; Called once at the end of the program → 1; Called inside a loop over range(1, 6) → 5; Called twice, and again inside a loop over range(1, 5) → 6; Called inside a loop over range(1, 3) that itself sits inside a loop over range(1, 5) → 8
- 3. 6
- 4. 4
- 5. 20
- 6. 6
- 7. a) 0
- 8. 8