Subroutines · Library Routines · Maintainability · Arrays · File Handling. The building blocks that turn short scripts into real, well-organised programs.
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 with its data type. Up to three can be used. |
| 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 | A ready-made routine provided by the language: MOD, DIV, ROUND, RANDOM. |
| MOD | Remainder after integer division. MOD(17, 5) = 2. |
| DIV | Integer division (discards remainder). DIV(17, 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. The declaration shows the first index (usually 1). |
| Text file | A file storing characters, read/written one line at a time. |
| File mode | How a file is opened: FOR READ (to read from it) or FOR WRITE (to write to it, replacing its contents). |
| 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) | Result ← Name(args) — used inside an expression |
| Keyword | PROCEDURE ... ENDPROCEDURE | FUNCTION ... RETURN ... ENDFUNCTION |
| Routine | What it does | Example |
|---|---|---|
| MOD(a, b) | Remainder | MOD(17, 5) → 2 |
| DIV(a, b) | Integer divide | DIV(17, 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, SUBSTRING, LCASE and UCASE are string handling functions — they are covered in the Decisions & Patterns lab.
| Syllabus feature | Why it helps future readers |
|---|---|
| Meaningful identifiers | StudentAge is obvious. X is not. |
| Comments | Explain what a section of code does, and why. |
| Procedures and functions | Each task is in a named, self-contained block that can be reused and tested on its own. |
Also good practice: indentation and white space (show the structure), and named constants (change a value such as TaxRate in one place).
| 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 (replaces the contents) | OPENFILE "data.txt" FOR WRITE |
| Read one line into a variable | READFILE "data.txt", Line |
| Write a variable as one line | WRITEFILE "data.txt", Line |
| Close file | CLOSEFILE "data.txt" |
WHILE NOT EOF("data.txt")). These are not part of the syllabus pseudocode, so exam questions won't require them.FUNCTION AreaOfBox(Length : INTEGER, Width : INTEGER, Height : INTEGER) RETURNS INTEGER
RETURN Length * Width * Height
ENDFUNCTION
// Main program
DECLARE BoxVolume : INTEGER
BoxVolume ← AreaOfBox(3, 4, 5) // BoxVolume is now 60
OUTPUT BoxVolume
Why it matters: Procedures and functions can have up to three parameters. Each parameter has a name and a data type, and the header ends with RETURNS and the type of the value sent back.
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
// read the first three lines and output them
OPENFILE "notes.txt" FOR READ
FOR Count ← 1 TO 3
READFILE "notes.txt", Line
OUTPUT Line
NEXT Count
CLOSEFILE "notes.txt"
// write a new line (replaces the old contents)
OPENFILE "backup.txt" FOR WRITE
WRITEFILE "backup.txt", Line
CLOSEFILE "backup.txt"
Pattern: OPENFILE (FOR READ or FOR WRITE) → READFILE or WRITEFILE → CLOSEFILE. Learn it as one unit.
A function returns a value — you must use it: Result ← MyFunc(5). Writing MyFunc(5) 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 whole-number part, MOD gives the remainder: DIV(17, 5) = 3, MOD(17, 5) = 2. Use MOD to test divisibility: IF MOD(Number, 2) = 0 checks if Number is even.
Maintainability is about making code READABLE, not correct. Validation checks input; loops repeat code. Neither improves maintainability. Say: meaningful identifiers, comments, and procedures/functions (indentation and constants also help).
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.
(Length : INTEGER, Width : INTEGER, Height : INTEGER).CALL ProcName(Value). The 2025 w25 examiner report noted few candidates used CALL correctly.DECLARE Name : ARRAY[lower:upper] OF TYPE. Colon separator, bounds in brackets. The lower bound is the first index (usually 1).IF MOD(Number, 2) = 0 is the standard "is even" test.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.
This lab 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: The longer programming question at the end of Paper 2 often uses arrays and subroutines, and can include files.
Discussion: Which of the 5 ideas 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 : STRING)
OUTPUT "Hi ", Name
ENDPROCEDURE
CALL Greet("Sam")
Calculates a value. Returns it via RETURN. Called inside an expression.
FUNCTION Double(Number : INTEGER) RETURNS INTEGER
RETURN Number * 2
ENDFUNCTION
Answer ← Double(7) // Answer 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 pattern you can step through and explore.
Click each improvement to apply it. The first three are the syllabus maintainability features; the last two are extra good practice.
Copy these skeletons and fill in the blanks. They follow the syllabus pseudocode style.
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. Give the syllabus features: meaningful identifiers · comments · procedures and functions (indentation and constants also help).
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.
OPENFILE … FOR WRITE replaces everything already in the file. To get data out of a file, open it FOR READ and use 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. In FUNCTION Square(Number : INTEGER) RETURNS INTEGER, Number is the parameter; Square(5) passes the 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.
Rapid questions on subroutines, arrays, library routines and files. Answer as many as you can in 60 seconds.
Press Start to begin the sprint.
Questions are weighted — weaker skills come up more often, and there are no immediate repeats.
Written questions in the Paper 2 style. Type your answer, mark it, then compare it with the model answer.
10 traps to memorise.
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.
Procedures are called with CALL. In one recent session, "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 = meaningful identifiers · comments · procedures and functions (plus indentation and constants as good practice).
Cited: 2023 s23 — "Many candidates suggested verification, validation and the use of loops"ROUND takes two arguments: ROUND(Value, Places). Leaving out the number of places is incomplete.
Common exam mistakeIf a function returns something, use it: Answer ← Calc(Value). Writing just Calc(Value) 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 questionExaminers expect meaningful identifiers — avoid 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.
That's the end of Topic 8: Programming. Next, Topic 9 covers databases: tables, records, fields, primary keys and SQL.