Cambridge O Level Computer Science · Syllabus 2210 · Programming
Function
What is Function?
A named block of statements that performs a task and returns a single value of a declared data type to the point at which it was called; it is called inside an expression and never with CALL.
This definition is part of the Programming chapter in Cambridge O Level Computer Science.
Function in context
A program is a stored sequence of instructions that manipulates data held in named storage. Topic 8 asks you to do four things with that idea. First, name and type your data correctly: a variable whose value may change, a constant whose value may not, each with one of the five data types INTEGER, REAL, CHAR, STRING, BOOLEAN. Second, control the order in which instructions run using only three structures — sequence, selection (IF, CASE) and iteration (FOR, WHILE, REPEAT). Third, break a long solution into procedures and functions so it stays readable and testable. Fourth, hold many related values in an array and hold them beyond the end of the run in a file. Everything else in this chapter is detail hanging off those four moves.
A statement is nested when it sits entirely inside the block of another statement. The four combinations you must handle are selection inside selection, selection inside iteration, iteration inside selection and iteration inside iteration. The syllabus states that candidates will not be required to write more than three levels of nested statements — so three is the ceiling for anything you produce, and if your answer is heading for four, the right move is to pull the inner part out into a procedure or function.
A procedure is a named block of statements that performs a task. It is started with CALL <identifier>, which is a complete program statement; when it finishes, control returns to the line that follows the procedure call. A function works in the same way except that it returns a single value to the point at which it was called, so its definition includes the data type of the value returned. Because a function call produces a value, it is not a complete statement: it must be called as part of an expression, and the keyword CALL must not be used. Both may take up to three parameters, and both are defined at the start of the code.
A local variable is declared inside a procedure or function and is available only within that routine. A global variable is declared outside all routines and is available throughout the program, including inside the routines. A routine's parameters behave as local variables too. Local variables are preferred because they cannot be changed by accident from somewhere else in the program, which makes a routine easier to test on its own; too many globals make a program harder to understand, harder to test and harder to change safely.
A maintainable program is one that another programmer — or you, six months later — can read, understand and safely change. The syllabus names the ingredients: meaningful identifiers for variables, constants, arrays, procedures and functions; relevant and appropriate use of the commenting feature provided by the language; and the use of procedures and functions to break the solution into named pieces. Consistent indentation and the removal of unnecessary repeated code follow from those. The syllabus lists it as a learning outcome of its own, 8.1.8, so it is something you can be asked about directly as well as something to practise in every answer you write.
Reading an algorithm that does not work, saying what is wrong with it and correcting it is a skill this topic leans on: the syllabus expects candidates to have practical programming experience, and every construct in Chapter 8 can be got subtly wrong. Two kinds of defect appear. A syntax defect breaks the rules of the notation: == for equality, a missing ENDIF, CALL in front of a function, infix MOD. A logic defect is perfectly legal pseudocode that does the wrong thing: an uninitialised total, a loop that runs one time too many, a counter outside its guard, a file opened in the wrong mode. Logic defects are much harder to spot by reading — which is why you trace.
Common mistakes with Function
- M11. “DIV and MOD are written between the two numbers.” TruthThe current specification writes both as function calls: DIV(10, 3) returns 3 and MOD(10, 3) returns 1. Why you have seen otherwiseThe infix forms 10 DIV 3 and 10 MOD 3 are older; the operator names were updated during this syllabus cycle. Follow a question's own code if it uses the old style; write the current one otherwise.
- M15. “The more deeply nested my solution, the more sophisticated it is.” TruthCandidates will not be required to write more than three levels of nested statements. Four levels signals an over-complicated design, not a clever one. FixMove the innermost block into a procedure or function, or replace a chain of nested IFs on one identifier with a flat CASE OF.
- M16. “A procedure gives a value back, just like a function.” TruthA procedure performs a task and returns no value. Only a function returns a value, and exactly one, of a declared data type. SymptomX ← PrintLine(5) — there is nothing for X to receive.
- M17. “You start a function with CALL, like a procedure.” TruthThe keyword CALL must not be used when calling a function. A function call is not a complete statement; it must appear as part of an expression. CorrectArea ← AreaOfRectangle(4.0, 2.5), or OUTPUT AreaOfRectangle(4.0, 2.5), or inside an IF condition.
- M19. “A local variable can be used anywhere in the program.” TruthA local variable is declared inside a procedure or function and is available only within that routine. Elsewhere the identifier refers to nothing. AlsoTwo routines cannot see each other's locals, and a local with the same name as a global is a different store.
- M20. “Making everything global is good practice — it saves passing things around.” TruthIt makes writing quicker and debugging slower. Any routine can change a global, so when one holds the wrong value every routine is a suspect, and no routine can be tested on its own. BetterPass values in as parameters and hand results back with a function's return value. Use a constant for a fixed value that several routines need.
Examiner tips on Function
- Version history you should know about, because old material contradicts it. Two things in the pseudocode specification changed during this syllabus cycle, and textbooks and worksheets printed before them are still in circulation. Integer division operators became function calls. Version 3 (January 2025) and version 4 (June 2025) both record that “the names of operators” were updated. The current specification writes DIV(<identifier1>, <identifier2>) and MOD(<identifier1>, <identifier2>), with the examples DIV(10, 3) returns 3 and MOD(10, 3) returns 1. The older infix forms 10 DIV 3 and 10 MOD 3 appear in a great deal of older material. This chapter uses the current function form throughout, and quotes the old form only to correct it. Parameter count was fixed at three. Version 4 records that “the number of parameters for a function and procedure has been updated”. Topic 8.1.6 now states that procedures and functions may have up to three parameters. Do not design an answer that needs four. Constants are declared with the arrow. The current specification gives the form CONSTANT <identifier> ← <value>, with the examples CONSTANT HourlyRate ← 6.50 and CONSTANT DefaultText ← "N/A". Some earlier material writes CONSTANT HourlyRate = 6.50. This chapter uses the arrow form, because it is what the current specification prints — so it is what you should train. Version 5 itself (December 2025) changed only a weblink on page 50. No Topic 8 content and no pseudocode convention changed between version 4 and version 5.
- DIV and MOD are written as function calls in the current specification. The forms are DIV(<identifier1>, <identifier2>), which “returns the quotient of identifier1 divided by identifier2 with the fractional part discarded”, and MOD(<identifier1>, <identifier2>), which “returns the remainder of identifier1 divided by identifier2”. Both identifiers are of data type integer. The specification's own examples are DIV(10, 3) returns 3 and MOD(10, 3) returns 1. The infix forms 10 DIV 3 and 10 MOD 3 come from an earlier version and from other languages. Use the function form. Follow a question's own pre-printed code if it uses the older style, but write the current one when the code is yours.
- Three specification details students skip. (1) Procedures and functions are defined at the start of the code, before the main program that uses them. (2) The RETURN statement is normally the last statement in the function definition. (3) Up to three parameters — if your design needs a fourth, redesign it. And the rule that catches most people in a written answer: the keyword CALL must not be used when calling a function.
Questions students ask about Function
What is the difference between a procedure and a function?
A procedure is a named block of statements that performs a task and returns no value; it is started with the CALL statement, and control returns to the line after the call. A function performs a task and returns exactly one value of a declared data type to the point where it was called, so it is used inside an expression, such as an assignment or OUTPUT statement, and the keyword CALL must never be used with it. Both may take up to three parameters.
Why are local variables generally preferred over global variables?
A local variable is declared inside a procedure or function and is available only within that routine, so it cannot be changed by accident from anywhere else in the program, which makes the routine easier to test on its own. A global variable is declared outside all routines and is available throughout the program, including inside every routine, so any routine can change it — when a global holds the wrong value, every routine that touches it becomes a suspect, and none can be tested in isolation.

