1. Procedures
A procedure is a module written once, between `PROCEDURE` and `ENDPROCEDURE`, and run when it is called. The procedure heading and ending are the first marks in a mark scheme.
A procedure carries out a task. In the papers it prompts and inputs values, tests them and outputs a message (`IsRA()`), outputs a result (`Calculate()`), or outputs a new string (`GetInitials()`). A function returns a (single) value and a procedure does not. A procedure is called by a statement of its own: `CALL IsRA()`.
Use a procedure where the same steps are needed in several places. Write them once as a module and replace each copy with a call. The module is designed and tested once, and any change needs to be made once only. The declaration of all variables used goes after the heading, so that they are local to the procedure.
Examiner insight
Worked example 15 marks
A triangle has sides of length A, B and C. In this example, A is the length of the longest side.
This triangle is said to be right-angled if the following equation is true:
`A × A = (B × B) + (C × C)`
A procedure will be written to check whether three lengths represent a right-angled triangle. The lengths will be input in any sequence.
The procedure IsRA() will:
• prompt and input three integer values representing the three lengths
• test whether the three lengths correspond to the sides of a right-angled triangle
• output a suitable message.
The length of the longest side may not be the first value input.
Write pseudocode for the procedure IsRA().
- 1
Procedure heading and ending, and declaration of all variables used: `PROCEDURE IsRA()`, `DECLARE a, b, c : INTEGER`, `ENDPROCEDURE` [1]
- 2
Appropriate prompt and input for each length: `OUTPUT "Input length of the first side"`, `INPUT a`, and the same for `b` and `c` [1]
- 3
One correct length test: `a * a = (b *b) + (c * c)` [1]
- 4
All three length tests, joined by `OR`, because any of the three could be the longest side [1]
- 5
Output one of two messages, following a reasonable attempt at a length test: `"It is right-angled"` or `"Not right-angled"` [1]