1. From a design to pseudocode
A design is given as a program flowchart or as structured English. Pseudocode writes the same steps in the Cambridge style: keywords in capitals, `←` for assignment, and indented blocks.
A function earns marks for its function header, with the parameters and the `RETURNS` type, and for the declaration of required local variable(s). It then needs the calculation, a `RETURN` so that the result is returned, and the closing `ENDFUNCTION`. A procedure closes with `ENDPROCEDURE`.
A value that the design passes in is a parameter. It is not read with `INPUT` inside the subroutine. `INPUT` and `OUTPUT` belong to the main program.
Examiner insight
Worked example 14 marks
Electric light bulbs are advertised online at 1.75 dollars each or 20 bulbs for 30.00 dollars.
A pseudocode function `BulbCost()` takes the quantity of bulbs purchased as its single parameter and returns the total cost in dollars.
Write pseudocode for the function `BulbCost()`.
- 1
Function header with a single parameter and the return type, ended by `ENDFUNCTION`: `FUNCTION BulbCost(Quantity : INTEGER) RETURNS REAL` [1]
- 2
Declaration of the local variables needed: `DECLARE Cost : REAL`, `DECLARE Twenty : INTEGER`, `DECLARE Other : INTEGER` [1]
- 3
Multiples of 20 and the other bulbs found with `DIV` and `MOD`: `Twenty ← Quantity DIV 20`, `Other ← Quantity MOD 20`, `Cost ← (Twenty * 30) + (Other * 1.75)` [1]
- 4
Cost is returned: `RETURN Cost` [1]
- 5
``` FUNCTION BulbCost(Quantity : INTEGER) RETURNS REAL DECLARE Cost : REAL DECLARE Twenty : INTEGER DECLARE Other : INTEGER Twenty ← Quantity DIV 20 Other ← Quantity MOD 20 Cost ← (Twenty * 30) + (Other * 1.75) RETURN Cost ENDFUNCTION ```