Sub-routines and a project
Answers are for parents and teachers.
1
Explain what a sub-routine is and give two reasons why programmers use them.
A sub-routine is a named group of steps, kept apart from the main program, that runs whenever it is called. Reasons: the steps are written once and reused; the main program is shorter and easier to follow; a change is made in one place.
2
The sub-routine stars shows ‘***’. Trace the flowchart for the name Ben. Write each line of output.
Three lines: ***, then Hello Ben, then ***. The sub-routine runs before and after the greeting.
3
A program has these steps: 1 Draw a square. 2 Move right. 3 Draw a square. 4 Move right. 5 Draw a square. Name a sub-routine and write the main program that uses it.
Sub-routine ‘draw a square’. Main: run draw a square, move right, run draw a square, move right, run draw a square. The three squares are drawn by the same steps, written only once.
4
Work out the expected outcomes by hand. The pocket-money planner multiplies the weekly amount by the number of weeks and shows ‘You can buy it’ if the result is at least the price.
| Test | Test data | Expected outcome |
|---|---|---|
| 1 | weekly = 3, weeks = 6, price = 15 | ? |
| 2 | weekly = 2, weeks = 6, price = 15 | ? |
| 3 | weekly = 5, weeks = 4, price = 20 | ? |
Test 1: saved 18, You can buy it. Test 2: saved 12, Keep saving. Test 3: saved 20, You can buy it (boundary).
5
Think of a small program you could write with one input, one calculation and one choice. Write its requirements, draw its flowchart and write two test-plan rows.
Answers will vary. A good answer has a clear requirement, a flowchart with input, process, a decision and two outputs, and two tests whose expected outcomes are worked out by hand (one of them a boundary test).