🔒

Pro Lesson

This lesson is part of the Pro plan. Upgrade to unlock all Cambridge IGCSE lessons.

£7.99/month
or £59/year
Subscribe now →← Back to Dashboard
📘 Paper 2 · Topic 10: Programming
10.1b Subroutines and Structured Programming
Cambridge IGCSE Computer Science 0478 · ~14 min read · ⭐ Pro

Structured Programming and Decomposition

Structured programming means writing code in a clear, logical way using sequence, selection, and iteration — and breaking large problems into smaller, manageable subproblems. This makes programs easier to write, test, and maintain.

Decomposition is the process of breaking a complex problem into smaller sub-problems that can each be solved independently. Each sub-problem becomes a subroutine.

Benefits of Using Subroutines

BenefitExplanation
Avoid repetitionWrite the code once and call it multiple times — no copy-paste errors
Easier testingEach subroutine can be tested independently
ReadabilityMain program is shorter and easier to understand
TeamworkDifferent programmers can write different subroutines
ReusabilitySubroutines can be used in other programs

Procedures

A procedure performs a task but does NOT return a value. It is called using the CALL keyword.

PROCEDURE Greet(name : STRING)
    OUTPUT "Hello, " & name & "!"
ENDPROCEDURE

CALL Greet("Alice")    // outputs: Hello, Alice!

Procedure with no parameters

PROCEDURE PrintLine()
    OUTPUT "-------------------"
ENDPROCEDURE

CALL PrintLine()
OUTPUT "Student Report"
CALL PrintLine()

Functions

A function performs a calculation and returns a value to where it was called. The return type is declared after RETURNS. You use the function in an expression (not with CALL).

FUNCTION Square(n : INTEGER) RETURNS INTEGER
    RETURN n * n
ENDFUNCTION

result ← Square(5)     // result = 25
OUTPUT Square(4) + 1   // outputs: 17

Function example: finding maximum

FUNCTION Max(a : INTEGER, b : INTEGER) RETURNS INTEGER
    IF a > b THEN
        RETURN a
    ELSE
        RETURN b
    ENDIF
ENDFUNCTION

largest ← Max(12, 7)   // largest = 12

Procedures vs Functions — Comparison Table

FeatureProcedureFunction
Returns a value?NoYes (declared with RETURNS)
How to callCALL name(args)name(args) in an expression
Keyword to endENDPROCEDUREENDFUNCTION
Main usePerform an action (e.g., output, write to file)Compute and return a result
RETURN statementNot usedRequired — returns the value

Parameters

Parameters are values passed into a subroutine. They are listed in brackets after the subroutine name with their data types: name : DATATYPE. Multiple parameters are separated by commas.

PROCEDURE PrintMultiple(message : STRING, times : INTEGER)
    DECLARE i : INTEGER
    FOR i ← 1 TO times
        OUTPUT message
    NEXT i
ENDPROCEDURE

CALL PrintMultiple("Hello!", 3)
// outputs: Hello! / Hello! / Hello!

Structured Program Example — Student Report

// Main program — uses subroutines for each section
DECLARE name : STRING
DECLARE score : INTEGER

CALL GetStudentData(name, score)
CALL PrintReport(name, score, Grade(score))

// Subroutines:
PROCEDURE GetStudentData(n : STRING, s : INTEGER)
    OUTPUT "Enter student name:"
    INPUT n
    OUTPUT "Enter score (0-100):"
    INPUT s
ENDPROCEDURE

FUNCTION Grade(s : INTEGER) RETURNS STRING
    IF s >= 70 THEN
        RETURN "A"
    ELSE IF s >= 60 THEN
        RETURN "B"
    ELSE IF s >= 50 THEN
        RETURN "C"
    ELSE
        RETURN "Fail"
    ENDIF
ENDFUNCTION

PROCEDURE PrintReport(n : STRING, s : INTEGER, g : STRING)
    OUTPUT "Student: " & n
    OUTPUT "Score: " & s
    OUTPUT "Grade: " & g
ENDPROCEDURE
Exam tip: In Paper 2, you may be asked to write a subroutine from a description, or trace through a subroutine call with given parameter values. Remember: procedures use CALL, functions are used in expressions. Always match ENDPROCEDURE/ENDFUNCTION with the correct opening keyword. Never use CALL with a function.
⚠️ Common Mistakes with Subroutines
  • Writing CALL for a function — functions are used in expressions (x ← Square(5)), not called with CALL
  • Forgetting RETURN in a function — the function must have a RETURN statement
  • Using ENDFUNCTION instead of ENDPROCEDURE or vice versa
  • Not listing the data type for parameters (e.g., writing PROCEDURE Greet(name) instead of PROCEDURE Greet(name : STRING))
  • Declaring variables inside a subroutine that are already declared in the main program (local vs global scope confusion)
Video coming soon
Click slide or press arrow keys to navigate
✍️

Worksheet — Subroutines

3 questions · 11 marks

Q1Write a procedure called DrawBox that takes a parameter width (INTEGER) and outputs a row of * characters of that width. [3]
✅ Mark scheme
PROCEDURE DrawBox(width : INTEGER) [1]; DECLARE i : INTEGER; FOR i ← 1 TO width [1]; OUTPUT "*"; NEXT i [1]; ENDPROCEDURE. Award mark for correct loop within correct procedure structure.
Q2Write a function called IsEven that takes one INTEGER parameter and returns TRUE if the number is even, FALSE otherwise. Then show how it would be used. [4]
✅ Mark scheme
FUNCTION IsEven(n : INTEGER) RETURNS BOOLEAN [1]; IF n MOD 2 = 0 THEN RETURN TRUE ELSE RETURN FALSE ENDIF [2]; ENDFUNCTION [1]. Usage: IF IsEven(x) THEN... or result ← IsEven(4).
Q3State two benefits of using subroutines in a program and explain one difference between a procedure and a function. [4]
✅ Mark scheme
Any two from: avoids repetition [1]; easier to test independently [1]; improves readability [1]; easier teamwork [1]; reusability [1]. Difference: a function returns a value (used in an expression); a procedure does not return a value (called with CALL) [1].
Quiz — Subroutines
Q 1 of 7
Score
/ 7
Click to reveal
TermDefinition
🎯

Mini Test — Subroutines

10 minutes · mixed marks

← 10.1a Full Programs Topic 10: Programming Next: 10.2 Testing →