Class 8 Computer Science — 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.This program prints four numbers. Put them in the order they appear on the screen.
def middle(): print(9) print(5) print(7) middle() print(1)- 1
- 9
- 5
- 7
2.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()3.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
- 5
- 6
- 8
- 0
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.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) 5
- d) 1
6.One named block calls another. How many numbers does it print?
def inner(): print(1) def outer(): inner() inner() outer()7.The named block below is called twice. How many numbers does it print?
def show(): print(1) print(2) show() show()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 — Class 8 Computer Science — Procedures, and calling them
- 1. 1. 7 2. 9 3. 5 4. 1
- 2. 6
- 3. 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
- 4. 4
- 5. a) 0
- 6. 2
- 7. 4
- 8. 8