Class 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.Two calls happen one after the other here. What number does it print?
def treble(x): return x * 3 a = treble(3) b = treble(a) print(b)2.This procedure multiplies and then adds. What number does it print?
def cost(n): return n * 5 + 2 print(cost(6))3.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(45)
- c) grow(60)
- d) grow(55)
4.One call sits inside another here. What number does it print?
def double(x): return x * 2 print(double(double(3)))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.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))7.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))8.This procedure takes something off what it is handed. What number does it print?
def less(x): return x - 5 print(less(30))
Answer key — Class 8 Computer Science — Procedures that take values and give one back
- 1. 27
- 2. 32
- 3. a) grow(50)
- 4. 12
- 5. 10
- 6. 15
- 7. 13
- 8. 25