Grade 8 Computer Science — Procedures that take values and give one back
Read a procedure that is handed values to work on, and work out what a call to it gives.
Name: ________________________
1.A procedure named grow takes a number and gives back that number plus a tenth of it. Which of these calls gives 55?
- a) grow(50)
- b) grow(60)
- c) grow(45)
- d) grow(55)
2.This procedure has a loop inside it. What number does it print?
def upto(n): total = 0 for k in range(1, n + 1): total = total + k return total print(upto(5))3.This procedure is handed two values rather than one. What number does it print?
def add(a, b): return a + b print(add(4, 9))4.A procedure named triple takes one number and gives back three times it. Sort each call by whether the value it gives is more than 20.
Groups: More than 20 · Less than 20
- triple(5)
- triple(6)
- triple(10)
- triple(8)
- triple(2)
- triple(7)
5.Follow this code by hand. What is total at the end?
def add(a, b): return a + b total = 0 for n in range(1, 5): total = add(total, n)6.One call sits inside another here. What number does it print?
def double(x): return x * 2 print(double(double(3)))7.A procedure named step takes a number and gives back 100 take away that number multiplied by itself. Put these calls in order of the value each one gives, smallest first.
- step(2)
- step(7)
- step(3)
- step(5)
8.A procedure named area takes a length and a width and gives back one multiplied by the other. Match each call to the value it gives.
- area(3, 4)
- area(5, 5)
- area(2, 9)
- area(6, 7)
- area(1, 8)
- 42
- 12
- 8
- 25
- 18
Answer key — Grade 8 Computer Science — Procedures that take values and give one back
- 1. a) grow(50)
- 2. 15
- 3. 13
- 4. triple(5) → Less than 20; triple(8) → More than 20; triple(7) → More than 20; triple(6) → Less than 20; triple(10) → More than 20; triple(2) → Less than 20
- 5. 10
- 6. 12
- 7. 1. step(7) 2. step(5) 3. step(3) 4. step(2)
- 8. area(3, 4) → 12; area(5, 5) → 25; area(2, 9) → 18; area(6, 7) → 42; area(1, 8) → 8