Explain where in the construction of an algorithm it would be appropriate to use a procedure, and relate this to the stages of the Program Development Life‑Cycle.
| Topic (9618) | Covered? |
|---|---|
| 11.1 Programming basics (declaration, assignment, I/O, built‑in functions) | ✅ |
| 11.2 Structured constructs (IF, CASE, loops) | ✅ |
| 11.3 Structured programming – procedures | ✅ |
| 12.1 Program Development Life‑Cycle (design → implementation → testing) | ✅ |
| 12.3 Program testing & maintenance | ✅ |
| A‑Level 20.1 Programming paradigms (functions, recursion, exception handling) | ✅ (introductory) |
| A‑Level 19.2 Recursion (basic understanding) | ✅ (preview) |
Example (pseudocode)
PROCEDURE displayMessage(msg) // procedure – no return value
OUTPUT msg
END PROCEDURE
FUNCTION square(x) RETURNS INTEGER // function – returns a value
RETURN x * x
END FUNCTION
| Mechanism | How it works | Typical use | Pros / Cons |
|---|---|---|---|
| Pass‑by‑value | A copy of the argument is passed. | Simple data types (INTEGER, REAL, CHAR, BOOLEAN). | Safe – original variable cannot be altered; uses extra memory for the copy. |
| Pass‑by‑reference | A reference (address) to the original variable is passed. | Large structures, arrays, or when the procedure must modify the caller’s data. | Efficient – no copying; but the procedure can change the caller’s variable. |
Recursion is a technique where a procedure calls itself. It is useful for problems that can be defined in terms of a smaller instance of the same problem (e.g., factorial, Fibonacci, tree traversal). A recursive procedure must have:
Simple example
FUNCTION factorial(n) RETURNS INTEGER
IF n = 0 THEN
RETURN 1
ELSE
RETURN n * factorial(n‑1)
END IF
END FUNCTION
Some exam questions may ask you to anticipate an error (e.g., division by zero). In pseudocode you can indicate the handling with a TRY … CATCH style block, or simply with an IF‑check before the risky operation.
PROCEDURE divide(a, b) RETURNS REAL
IF b = 0 THEN
OUTPUT "Error: divisor cannot be zero."
RETURN 0 // or a sentinel value
END IF
RETURN a / b
END PROCEDURE
IF … THEN … ELSE … END IFCASE … OF … END CASEFOR i FROM 1 TO n DO … END FORWHILE condition DO … END WHILEREPEAT … UNTIL conditionREAD numberOfStudents
FOR i FROM 1 TO numberOfStudents DO
READ name, mark1, mark2
CALL processStudent(name, mark1, mark2) // placeholder
END FOR
Document before coding.
| Item | Description |
|---|---|
| Purpose | What the procedure does. |
| Parameters | Name, type, and passing mechanism (value/reference). |
| Return value | Type of value returned, or “none”. |
| Pre‑conditions | Conditions that must be true before the call. |
| Post‑conditions | What is guaranteed after the call. |
Write detailed steps, using the chosen parameter‑passing method and respecting scope rules.
Replace the placeholder with the concrete call, ensuring data dependencies are satisfied.
| Situation | Why a Procedure Helps | Example |
|---|---|---|
| Repeated calculations | Write the formula once; call it wherever needed. | Area of different shapes (square, circle, triangle). |
| Complex decision logic | Encapsulate nested IF/CASE statements, keeping the main flow clear. | Validating a student’s mark against several criteria. |
| Data manipulation on collections | Single point of change if the algorithm (e.g., sorting) needs to be altered. | Sorting a list of names, searching a record set. |
| Input/Output handling | Separate I/O from core processing, improving portability. | Reading a file, formatting a report. |
| Error handling / validation | Centralise checks, making the algorithm more robust. | Ensuring a score is between 0 and 100 before processing. |
This example shows a procedure that receives two marks, calculates the average, determines the grade, and returns both values as a record.
MAIN
READ numberOfStudents
FOR i FROM 1 TO numberOfStudents DO
READ studentName, mark1, mark2
result ← CALL analyseStudent(mark1, mark2) // result is a RECORD
OUTPUT studentName, result.average, result.grade
END FOR
END MAIN
/* Procedure specification
Purpose : Calculate average of two marks and assign a grade.
Parameters: m1 (INTEGER, pass‑by‑value)
m2 (INTEGER, pass‑by‑value)
Returns : RECORD { average (REAL), grade (CHAR) }
Preconditions: 0 ≤ m1 ≤ 100, 0 ≤ m2 ≤ 100
Postconditions: average = (m1+m2)/2,
grade follows the table below
*/
PROCEDURE analyseStudent(m1, m2) RETURNS RECORD
avg ← (m1 + m2) / 2.0
IF avg ≥ 80 THEN g ← 'A'
ELSE IF avg ≥ 70 THEN g ← 'B'
ELSE IF avg ≥ 60 THEN g ← 'C'
ELSE IF avg ≥ 50 THEN g ← 'D'
ELSE g ← 'F'
END IF
RETURN { average: avg, grade: g }
END PROCEDURE
Returned record fields
| Field | Content |
|---|---|
| average | Real number representing the mean of the two marks. |
| grade | Character ‘A’‑‘F’ based on the average. |
/*------------------------------------------------------------
Procedure name :
Purpose :
Parameters : name1 – type – passing mechanism (value/reference)
name2 – type – passing mechanism
Returns : type (or “none” for a procedure)
Preconditions :
Postconditions :
------------------------------------------------------------*/
READ, WRITE, OPENFILE, SORT, etc.Your generous donation helps us continue providing free Cambridge IGCSE & A-Level resources, past papers, syllabus notes, revision questions, and high-quality online tutoring to students across Kenya.