Explain at which stage in the construction of an algorithm it is appropriate to introduce a function, and justify how doing so improves the design, implementation, testing and maintenance of a program.
| Specification Section | Relevant Content |
|---|---|
| 11.3 Using Functions | Define, call, test functions; use parameters and return values. |
| 11.2 Constructs | Show that functions can contain IF…ELSE, CASE, and loop structures. |
| 11.1 Programming Basics | Variable/constant declaration, input‑output and scope rules for functions. |
| 12.1 Program Development Life‑cycle | Relate “function‑first” design to analysis, design, coding, testing and maintenance. |
| 12.3 Program Testing and Maintenance | Unit‑test individual functions before integration. |
| Assessment Objectives |
|
| Stage of Development | What to Do | Why a Function Helps |
|---|---|---|
| Problem Analysis | Identify distinct operations (e.g., “calculate average”, “sort list”). | Creates a clear mapping from the problem description to program modules. |
| Algorithm Design (pseudocode) | Replace repeated step sequences with a named sub‑algorithm. | Reduces pseudocode length and highlights logical structure. |
| Implementation (coding) | Write the function definition before the main routine (or in a separate module). | Ensures the main routine reads like a high‑level plan and respects scope rules. |
| Testing & Debugging | Unit‑test each function independently, then integrate. | Isolates faults and speeds up verification (AO3). |
| Maintenance | Modify a function when the underlying task changes. | Only one location needs updating, reducing the risk of new errors. |
| Element | Syntax |
|---|---|
| Function header | FUNCTION name(parameter‑list) → return‑type |
| Parameters | Comma‑separated, each with a type (e.g., list: LIST OF INTEGER) |
| Return statement | RETURN expression |
| End of function | END FUNCTION |
FUNCTION max(a: INTEGER, b: INTEGER) → INTEGER
IF a > b THEN
RETURN a
ELSE
RETURN b
END IF
END FUNCTION
FUNCTION swapIfGreater(x: INTEGER, y: INTEGER) → LIST OF INTEGER
IF x > y THEN
RETURN [y, x] // returns a list with the values swapped
ELSE
RETURN [x, y]
END IF
END FUNCTION
A function may contain any standard construct (IF, CASE, FOR, WHILE). Conversely, a function can be called from within those constructs.
FUNCTION isPrime(n: INTEGER) → BOOLEAN
IF n < 2 THEN RETURN FALSE END IF
FOR i FROM 2 TO FLOOR(SQRT(n))
IF n MOD i = 0 THEN RETURN FALSE END IF
END FOR
RETURN TRUE
END FUNCTION
FOR i FROM 1 TO 20
IF isPrime(i) THEN OUTPUT i, " is prime" END IF
END FOR
Problem statement: For each data set read from input, output its median. The steps “sort the list” and “pick the middle element” are repeated for every set.
READ sets
FOR s FROM 1 TO sets
READ size
READ size numbers INTO list
// bubble sort
FOR i FROM 1 TO size-1
FOR j FROM i+1 TO size
IF list[i] > list[j] THEN SWAP list[i], list[j] END IF
END FOR
END FOR
// pick median
IF size MOD 2 = 1 THEN
median ← list[(size+1)/2]
ELSE
median ← (list[size/2] + list[size/2+1]) / 2
END IF
OUTPUT median
END FOR
FUNCTION sortAscending(data: LIST OF INTEGER) → LIST OF INTEGER
FOR i FROM 1 TO LENGTH(data)-1
FOR j FROM i+1 TO LENGTH(data)
IF data[i] > data[j] THEN SWAP data[i], data[j] END IF
END FOR
END FOR
RETURN data
END FUNCTION
FUNCTION median(data: LIST OF INTEGER) → REAL
SET sorted ← sortAscending(data)
SET n ← LENGTH(sorted)
IF n MOD 2 = 1 THEN
RETURN sorted[(n+1)/2]
ELSE
RETURN (sorted[n/2] + sorted[n/2+1]) / 2
END IF
END FUNCTION
// Main program
READ sets
FOR s FROM 1 TO sets
READ size
READ size numbers INTO numbers
OUTPUT median(numbers)
END FOR
sortAscending needs to be changed.sortAscending, test:
median, test:
[3,1,4] → 3).[2,8,5,7] → 6.0).| AO | What the student must demonstrate |
|---|---|
| AO1 | Define “function”, “parameter”, “return value”, “scope”, and explain the benefits of encapsulation. |
| AO2 | Apply the decision checklist to select appropriate points in an algorithm to introduce a function; write correct Cambridge‑style pseudocode. |
| AO3 | Design a solution that uses functions to improve readability, re‑use and testability; justify the placement of each function within the development life‑cycle. |
public static int max(int a, int b) in Java).return and ensure the declared return type matches.TRY…CATCH inside functions to manage run‑time errors.median → sortAscending. Each block is labelled with its function name to visualise encapsulation.Create an account or Login to take a Quiz
Log in to suggest improvements to this note.
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.