Subroutines · Library Routines · Maintainability · Arrays · File Handling. The architectural elements that turn code into software students can be proud of.
Every real program uses these five ideas together. Subroutines package tasks. Library routines save you from re-inventing common operations. Maintainability makes code readable to future you. Arrays store many values under one name. File handling makes data survive when the program closes. This is where you stop writing tiny scripts and start writing software.
| Term | Meaning |
|---|---|
| Procedure | A named block of code that performs a task but does not return a value. |
| Function | A named block of code that returns a single value using RETURNS. |
| Parameter | A named input to a subroutine, listed in brackets. Up to 3 allowed (2026 syllabus). |
| Argument | The actual value passed to a parameter when the subroutine is called. |
| Global scope | A variable declared outside any subroutine — accessible everywhere. |
| Local scope | A variable declared inside a subroutine — only exists in that subroutine. |
| Library routine | Pre-built function provided by the language (LENGTH, ROUND, RANDOM etc.). |
| MOD | Remainder after integer division. 17 MOD 5 = 2. |
| DIV | Integer division (discards remainder). 17 DIV 5 = 3. |
| 1D array | A list of values under one name, accessed by one index. Scores[3] |
| 2D array | A table of values with rows and columns. Grid[Row, Col] |
| Index | The position number of an array element. Cambridge indexes start at 1 by default. |
| Text file | A file storing characters, read/written one line at a time. |
| EOF | End Of File — the marker used to stop reading a file. |
| Meaningful identifier | A name that describes the value: StudentAge, not x. |
| Feature | PROCEDURE | FUNCTION |
|---|---|---|
| Purpose | Perform a task | Calculate and RETURN a value |
| Returns | Nothing | Exactly one value (RETURNS TYPE) |
| Called with | CALL Name(args) | x <- Name(args) inside an expression |
| Keyword | PROCEDURE ... ENDPROCEDURE | FUNCTION ... RETURN ... ENDFUNCTION |
| Routine | What it does | Example |
|---|---|---|
| MOD | Remainder | 17 MOD 5 → 2 |
| DIV | Integer divide | 17 DIV 5 → 3 |
| ROUND(x,d) | Rounds x to d decimal places | ROUND(3.567, 1) → 3.6 |
| RANDOM() | Random real between 0 and 1 | RANDOM() → 0.4271... |
| LENGTH(s) | Number of chars in string | LENGTH("Cat") → 3 |
| SUBSTRING(s,p,n) | n chars starting at position p (1-based) | SUBSTRING("Cambridge",1,3) → "Cam" |
| LCASE(s) | Lowercase copy of string | LCASE("HELLO") → "hello" |
| UCASE(s) | Uppercase copy of string | UCASE("hello") → "HELLO" |
| Technique | Why it helps future readers |
|---|---|
| Meaningful identifier names | StudentAge is obvious. x is not. |
| Comments | Explain WHY the code does something, not just what. |
| Indentation and white space | Shows structure at a glance. |
| Constants | Change TaxRate in one place, not 20. |
| Subroutines | Reusable, self-contained, easier to test. |
| Type | Declare | Access |
|---|---|---|
| 1D array | DECLARE Scores : ARRAY[1:10] OF INTEGER | Scores[3] <- 87 |
| 2D array | DECLARE Grid : ARRAY[1:5, 1:4] OF STRING | Grid[2, 3] <- "X" |
| Operation | Syntax |
|---|---|
| Open for reading | OPENFILE "data.txt" FOR READ |
| Open for writing (overwrites) | OPENFILE "data.txt" FOR WRITE |
| Open for appending | OPENFILE "data.txt" FOR APPEND |
| Read one line | READFILE "data.txt", Buffer |
| Write one line | WRITEFILE "data.txt", Content |
| Close file | CLOSEFILE "data.txt" |
| Detect end | WHILE NOT EOF("data.txt") ... ENDWHILE |
FUNCTION AreaOfBox(Length : INTEGER, Width : INTEGER, Height : INTEGER) RETURNS INTEGER
RETURN Length * Width * Height
ENDFUNCTION
// Main program
DECLARE V : INTEGER
V <- AreaOfBox(3, 4, 5) // V is now 60
OUTPUT V
Why it matters: The 2026 syllabus explicitly allows up to 3 parameters. Earlier syllabuses limited to 2. Learn this well — the s25 examiner report withdrew a question that required 3 parameters, so Cambridge has now updated the syllabus.
DECLARE Points : ARRAY[1:4, 1:3] OF INTEGER
DECLARE Total : INTEGER
Total <- 0
FOR Row <- 1 TO 4
FOR Col <- 1 TO 3
Total <- Total + Points[Row, Col]
NEXT Col
NEXT Row
OUTPUT Total
Trap: Cambridge examiner reports flag candidates who "did not use both dimensions" — students often iterate one row and forget the outer loop.
DECLARE Line : STRING
OPENFILE "notes.txt" FOR READ
WHILE NOT EOF("notes.txt")
READFILE "notes.txt", Line
OUTPUT Line
ENDWHILE
CLOSEFILE "notes.txt"
Pattern: Every file read follows this same 4-step shape: OPEN → WHILE NOT EOF → READ → CLOSE. Learn it as one unit.
A function returns a value — you must capture it: x <- MyFunc(). Writing MyFunc() alone throws the value away. Procedures don't have this problem because they don't return anything.
You cannot process all 200 members by referring to the array name alone. You need FOR i <- 1 TO 200 and access Members[i] inside. This is one of the most-cited exam errors.
DIV gives the quotient, MOD gives the remainder. 17 DIV 5 = 3, 17 MOD 5 = 2. Use MOD to test divisibility: IF x MOD 2 = 0 checks if x is even.
Maintainability is about making code READABLE, not correct. Validation checks input; loops repeat code. Neither improves maintainability. Say: comments, meaningful names, subroutines, indentation, constants.
Cited: 2023 s23 examiner report — "Many candidates suggested verification, validation and the use of loops"Every OPENFILE needs a matching CLOSEFILE. Files left open can lose data or block other programs. Cambridge mark schemes explicitly reward the CLOSEFILE line.
Cited: 2024 s24 examiner report — file handling questionsA 2D array needs BOTH indexes: Grid[Row, Col]. Students often iterate just rows and forget columns, or use Grid[Row] which isn't valid Cambridge syntax.
CALL ProcName(x). Just writing ProcName(x) may cost marks.DECLARE Name : ARRAY[lower:upper] OF TYPE. Colon separator, bounds in brackets. Cambridge indexes start at 1 by default.IF Number MOD 2 = 0 is the standard "is even" test. Learn this pattern.You know the syntax for procedures and functions, arrays (1D and 2D), library routines, and file handling. You can spot maintainability techniques. Head to Activities to try them hands-on.
Topic 8.12–8.16 is where a program stops being a script and becomes software. You'll build the tools that let programs grow: named blocks of code that you can call anywhere (subroutines), pre-built utilities (library routines), habits that keep code readable (maintainability), containers for lots of data (arrays), and permanent storage (files).
Context: These are Paper 2's hardest programming questions. The 15-mark question almost always involves at least an array and a subroutine, sometimes files too.
Discussion: Which of the 5 sub-topics do you already feel confident with? Which one worries you? Book Notes covers the theory; this Learn tab makes each one hands-on.
These two words describe subroutines, but they behave differently. Getting the choice right is worth marks.
Does a task. No return value. Called with CALL.
PROCEDURE Greet(Name)
OUTPUT "Hi ", Name
ENDPROCEDURE
CALL Greet("Sam")
Calculates a value. Returns it via RETURN. Called inside an expression.
FUNCTION Double(x)
RETURNS INTEGER
RETURN x * 2
ENDFUNCTION
y <- Double(7) // y is 14
Ask: does the caller need a result? If yes → function. If it just needs the task done → procedure.
Test: "Print welcome" = procedure. "Calculate area" = function.
Pick a mode. Each shows a working Cambridge pattern you can step through, poke at, and break.
Click each improvement to apply it. Watch the code transform. Watch the maintainability score climb.
Copy these skeletons. Fill in the blanks. These are the shapes examiners expect to see.
Every FUNCTION must declare what type it returns — RETURNS INTEGER, RETURNS STRING, etc. Without RETURNS TYPE, the mark scheme reads it as a procedure.
Validation is about correct input. Maintainability is about readable CODE. The 5 accepted techniques: comments · meaningful names · indentation · constants · subroutines.
Cited: 2023 s23 examiner report — verbatimEven if you write FOR i <- 1 TO 10, if you write INPUT Scores (no [i]) you're not using the loop counter. Use INPUT Scores[i].
A 2D array needs a nested loop: outer for rows, inner for columns. If you only loop rows, you access Grid[r, 1] for every row — same column repeated.
If you READFILE past the last line, the program crashes. Always WHILE NOT EOF("file.txt") before READFILE.
Variables declared inside a PROCEDURE or FUNCTION are LOCAL — they only exist during that call. Trying to OUTPUT them from main after the call fails.
Cited: 2024 m24 examiner report — scope questionsCambridge ROUND takes TWO arguments: value and decimal places. ROUND(3.567) is incomplete. Write ROUND(3.567, 1) → 3.6.
Parameters are declared in the subroutine header. Arguments are the actual values passed in the call. FUNCTION F(x) has parameter x; F(5) passes argument 5.
Each drill mixes easy, medium, and exam-level questions. Watch for the EXAM tag — those are the stretch questions.
A pseudocode snippet is shown. Say whether it's a procedure or a function — and why.
An array is shown with a value at some index. Predict what the code will output.
A library routine is called. What does it return?
Given a file task, pick the correct sequence of file operations.
10 rapid-fire questions on subroutines, arrays, and library routines. Timer starts on your first answer.
Press Start to begin the sprint.
Questions are weighted — weaker skills come up more often, and there are no immediate repeats.
Every question below is Cambridge-style with a paper citation. Type your answer, submit for keyword auto-marking, then reveal the model answer.
10 traps drawn from the last 3 years of examiner reports. Memorise these.
Cambridge often asks for a function that takes a value in. Writing FUNCTION F() RETURNS INTEGER when the question says "takes x as a parameter" costs the parameter mark AND breaks the calling code.
Cambridge mark schemes list CALL as an explicit expected keyword. Some sessions, "few were able to complete the task using the CALL command."
Cited: 2025 w25 Q2(b)(i) — verbatimIf the array has 200 elements, you need FOR i <- 1 TO 200. Referring to Members alone processes nothing.
2D arrays need both indexes and nested loops. Iterating Grid[r] without Grid[r, c] misses the second dimension.
Wrong. Maintainability = comments · meaningful names · indentation · constants · subroutines. Learn these 5 exactly.
Cited: 2023 s23 — "Many candidates suggested verification, validation and the use of loops"Cambridge ROUND = ROUND(value, dp). Missing the dp argument = missing marks.
Cited: Multiple sessions 2023–2025If a function returns something, use it: Answer <- Calc(x). Writing just Calc(x) throws the value away and often means the code is a procedure by mistake.
Every OPENFILE needs a CLOSEFILE. Mark schemes reward this explicitly.
Cited: 2024 s24 — file questionVariables declared inside PROCEDURE/FUNCTION are LOCAL. They don't exist in main after the call. Return the value if you need it in main.
Cited: 2024 m24 — scope questionCambridge marks off for x, y, data, Array, temp. Use StudentAge, TotalPrice, HighestScore.
Green = ≥70% accuracy over 3+ attempts. Yellow = 30–69%. Grey = untried. Tap a badge to override.