O Level Computer Science (2210)

Programming

Procedures, functions, and maintainable code

Write reusable subroutines with parameters, return values from functions, understand local and global variables, and keep code maintainable.

Examples use Cambridge pseudocode for the 2026–2028 syllabus.

Why subroutines?

A subroutine is a named block of code that does one job and can be called from anywhere in the program. Writing it once and calling it many times makes programs shorter, easier to test, and easier to change.

There are two kinds. A procedure does a task. A function does a task and returns a value to where it was called.

Procedures

Define a procedure with PROCEDURE ... ENDPROCEDURE and run it with CALL. Values passed in are parameters.

Pseudocode
PROCEDURE DrawLine(Size : INTEGER)
    DECLARE Count : INTEGER
    DECLARE Line : STRING
    Line <- ""
    FOR Count <- 1 TO Size
        Line <- Line & "-"
    NEXT Count
    OUTPUT Line
ENDPROCEDURE

CALL DrawLine(10)
OUTPUT "Menu"
CALL DrawLine(10)

Functions

A function states the type of value it returns with RETURNS, and sends the value back with RETURN. Because it gives back a value, you use a function inside an expression instead of with CALL.

  1. 1
    Call
    Area(4.5, 3) is reached.
  2. 2
    Parameters
    Width gets 4.5, Height gets 3.
  3. 3
    Run
    The function body calculates 13.5.
  4. 4
    Return
    13.5 replaces the call and is stored in RoomArea.
Pseudocode
FUNCTION Area(Width : REAL, Height : REAL) RETURNS REAL
    RETURN Width * Height
ENDFUNCTION

DECLARE RoomArea : REAL
RoomArea <- Area(4.5, 3)
OUTPUT "Carpet needed: ", RoomArea, " square metres"

Local and global variables

A local variable is declared inside a subroutine. It exists only while the subroutine runs and cannot be used outside it. Count and Line in DrawLine are local.

A global variable is declared in the main program and can be used anywhere, including inside subroutines.

Exam tip. Prefer local variables and parameters. Subroutines that only use their own variables are easier to test and reuse, and cannot accidentally change other parts of the program.

Writing maintainable programs

Programs are read far more often than they are written. Make yours easy for someone else (or you, next month) to understand and change:

  • Use meaningful identifiers: TotalScore instead of T.
  • Add comments with // to explain why the code does something.
  • Use procedures and functions so each part does one clear job.
  • Use constants for fixed values, and indent blocks consistently.
Pseudocode
// Works out the cost of a cinema trip
CONSTANT TicketPrice <- 8.5
DECLARE People : INTEGER
INPUT People
OUTPUT "Total cost: ", People * TicketPrice // before any discount

Check yourself

What is the difference between a procedure and a function?Show answer

A function returns a value; a procedure does not.

Write the header of a function IsEven that takes an integer and returns a Boolean.Show answer

FUNCTION IsEven(Number : INTEGER) RETURNS BOOLEAN.

Give two features that make a program maintainable.Show answer

For example: meaningful identifiers, comments, subroutines, constants, consistent indentation.

References used to build these lessons

We used these official documents as references when building this course's lessons. They are the sources for the syllabus structure and course requirements.

Cambridge International · 2210 · Exams 2026–2028