Parameters, local state and clear interfaces
A subprogram has a name and a defined task. A procedure performs a task; a function returns a value to its caller. Parameters provide inputs for a particular call. A clear interface states the expected inputs and returned result so the caller does not need to know every internal step.
A local variable belongs to a particular subprogram invocation and is normally accessible only there. A global variable can be accessed more widely. Local state reduces accidental interference and makes modules easier to test. A value stored by the caller after a return is separate from the ended invocation's temporary local variable.
Worked example: school equipment hire
- Supply hours 3 and rate 8 to Calculate charge. Its local charge becomes 3 × 8 = 24, but the caller has no returned result yet.
- Return 24 and end this invocation. The caller stores the returned charge, adds a separate global booking fee of 2 to obtain 26, and then displays 26.
- A fresh call receives hours 5 and rate 6. Its local charge begins unset, becomes 30 and is returned. The caller can retain the first result and calculate the second total as 30 + 2 = 32.
A display-only procedure could show 24 without supplying any returned charge. Screen output is not a return value, and no returned value does not mean zero.
Keep each input in its parameter's place
A parameter is a named input in a subprogram definition. A call supplies the values for one use of that subprogram. The first supplied value goes to the first parameter, the second to the second, and so on. Those roles matter even when both values have the same numeric type.
New worked example: a room has capacity 20 places and 7 are booked. Remaining(capacity, booked) returns capacity minus booked. Calling Remaining(20, 7) gives capacity the value 20 and booked the value 7, so it returns 13 places. Reversing the supplied values calls a different calculation: 7 minus 20 returns −13. The routine shown has no validation rule to reject this call; it performs the stated subtraction.
Multiplication can hide a swapped-input mistake because 3 × 8 equals 8 × 3. Subtraction exposes it. Use names and parameter positions to establish the interface, not the coincidence that one example produced the expected number. Remaining(6, 6) returns a valid zero: the room has no spare places, but the function did supply a result.
| Call | capacity | booked | Returned capacity minus booked |
|---|---|---|---|
| Remaining(20, 7) | 20 | 7 | 13 |
| Remaining(7, 20) | 7 | 20 | −13 |
| Remaining(6, 6) | 6 | 6 | 0 |
Trace the call, then the caller
In the equipment-booking example, CalculateCharge(hours, rate) calculates hours multiplied by rate. A local charge belongs to that active call. The caller cannot already use that local name as its own stored bookingCharge. A RETURN operation supplies the result to the caller, which can then store it, add a separate booking fee and request display.
For CalculateCharge(3, 8), bind hours to 3 and rate to 8. Calculate and store 24 locally, return 24, then store the returned value in bookingCharge. Adding the separate £2 booking fee gives a caller total of £26. Output is another operation: neither returning 24 nor storing 26 automatically displays anything.
A second call CalculateCharge(5, 6) uses fresh local values and returns 30. If the caller saves that result under secondCharge, its earlier bookingCharge remains 24. The function does not overwrite every caller variable just because it is reused. A procedure whose task is to display a heading can legitimately return no value, but that output cannot replace the returned number needed in a calculation.
One shared calculation can replace four duplicated implementations, so a corrected rule is applied through each caller. It still needs tests: a defect in shared code can affect all four callers. Test both the subprogram's inputs and expected return values, then how the caller uses the result. Modular structure does not itself guarantee faster execution.
| Event | Inside the active call | Caller / screen |
|---|---|---|
| Bind inputs | hours = 3; rate = 8 | No returned charge yet |
| Calculate and store locally | charge = 24 | Still no returned charge |
| Return and store | Call ends; local work is no longer active | bookingCharge stores 24 |
| Add separate fee | No call active | total stores 26; no display yet |
| Display total | No call active | Screen shows 26 only if output executes |
OCR focus
OCR: passing and returning arrays
An array can be a parameter or a returned result. State clearly whether a subprogram reads a supplied collection, changes it or produces a new one. Follow the behaviour given in the question; do not assume every language copies arrays or shares them in the same way.
Supplying the whole scores array is different from supplying scores[1], which selects one element. Returning an array is also different from returning its length. In the OCR model an array is fixed-length: creating three result slots and filling them does not make the array grow.
Worked example: a club awards bonus points
- Add bonus receives scores [6, 10, 4] and bonus 2. Its contract creates a NEW three-element result and leaves scores unchanged.
- Read each input in index order, add 2, and write only the corresponding result slot: 6 + 2 = 8; 10 + 2 = 12; 4 + 2 = 6. An unset result slot is not zero.
- Return the complete array [8, 12, 6]. The caller stores it, then reads index 1 to obtain 12. The returned length is 3, not 12.
- If the caller replaces result index 0 with 99, the original scores remain [6, 10, 4] under this NEW-array contract. A separate call using [1, 8, 3] and bonus 4 returns [5, 12, 7].
Keep the input array, returned array, selected element and length separate. A different stated contract could deliberately change its input instead.
Continue with your exam board
The course combines lesson videos, interactive activities, topic questions and original exam mocks. Sign in to use the learning tools and save your progress; your account’s course access applies.
Independent learning resources. Not endorsed by AQA or OCR. These public introductions complement the full course and the official specification.