📘 Paper 2 · Topic 8: Programming
8.3 Procedures and Functions
Cambridge IGCSE Computer Science 0478 · ~16 min read · ⭐ Pro

Subroutines: Procedures and Functions

A subroutine is a named block of code that performs a specific task. Subroutines allow code to be written once and called (used) many times. Cambridge IGCSE distinguishes two types: procedures and functions.

Procedures

A procedure is a subroutine that performs a task but does NOT return a value. It may accept parameters (input values) but it outputs results through side effects (e.g., OUTPUT statements) rather than returning a value.

Defining a procedure

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

Calling a procedure

CALL greet("Alice")   // Outputs: Hello, Alice
CALL greet("Bob")     // Outputs: Hello, Bob

Procedure without parameters

PROCEDURE drawLine()
    OUTPUT "-------------------"
ENDPROCEDURE

CALL drawLine()

Functions

A function is a subroutine that performs a task AND returns a value to the calling code. The return type must be specified in the definition. The RETURN keyword sends the result back.

Defining a function

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

Calling a function

result ← square(5)        // result = 25
OUTPUT square(4)          // Outputs 16
OUTPUT square(3) + 1      // Outputs 10

Function with a condition

FUNCTION isEven(num : INTEGER) RETURNS BOOLEAN
    IF num MOD 2 = 0 THEN
        RETURN TRUE
    ELSE
        RETURN FALSE
    ENDIF
ENDFUNCTION

Comparing Procedures and Functions

FeatureProcedureFunction
Returns a value?NoYes (using RETURN)
Return type in header?NoYes — RETURNS datatype
Called with?CALL procedureName()Used in an expression or assignment
ENDPROCEDURE / ENDFUNCTIONENDPROCEDUREENDFUNCTION
Typical usePrinting, modifying global dataCalculating and returning a result

Parameters

Parameters are values passed into a subroutine when it is called. They are listed in brackets after the subroutine name in the definition, with their data types specified.

FUNCTION add(a : INTEGER, b : INTEGER) RETURNS INTEGER
    RETURN a + b
ENDFUNCTION

OUTPUT add(3, 7)   // Outputs 10

Benefits of Using Subroutines

  • Avoid code duplication — write once, call many times
  • Easier to test and debug individual sections
  • Easier to read and understand (meaningful names)
  • Easier to maintain — change the code in one place
Exam tip: To call a procedure use CALL. To call a function, use it in an expression (e.g., x ← square(5)) — do NOT use CALL for functions. Always include RETURNS datatype in a function header. Remember ENDPROCEDURE / ENDFUNCTION (not just END).
⚠️ Common Mistakes
  • Using CALL for a function — functions are used in expressions, not called with CALL
  • Forgetting RETURNS datatype in a function header
  • Confusing ENDPROCEDURE with ENDFUNCTION
  • Not specifying parameter types in the definition (e.g., writing name instead of name : STRING)
Video coming soon
Click slide or press arrow keys to navigate
✍️

Worksheet — Procedures & Functions

4 questions · 11 marks

Q1Write a PROCEDURE called displayMenu that outputs "1. Play", "2. Settings", "3. Quit" on separate lines. Show how to call it. [3]
✅ Mark scheme
PROCEDURE displayMenu() [1]; OUTPUT "1. Play"; OUTPUT "2. Settings"; OUTPUT "3. Quit"; ENDPROCEDURE [1]; CALL displayMenu() [1]
Q2Write a FUNCTION called cube that takes an INTEGER n and returns n cubed. Show how to use it to store 3 cubed in a variable. [4]
✅ Mark scheme
FUNCTION cube(n : INTEGER) RETURNS INTEGER [2] (1 for header, 1 for RETURNS INTEGER); RETURN n * n * n; ENDFUNCTION; result ← cube(3) [2] (1 for valid call, 1 for assignment)
Q3State TWO differences between a procedure and a function. [2]
✅ Mark scheme
Any two: (1) A function returns a value; a procedure does not [1]; (2) A function uses RETURNS datatype in its header; a procedure does not [1]; (3) A function is called in an expression; a procedure is called with CALL [1]; (4) A function uses ENDFUNCTION; a procedure uses ENDPROCEDURE [1]
Q4Give TWO benefits of using subroutines (procedures/functions) in a program. [2]
✅ Mark scheme
Any two: Code only written once and reused — avoids duplication [1]; Easier to test individual sections [1]; Easier to read/understand using meaningful subroutine names [1]; Easier to maintain — changes made in one place [1]
Quiz — Procedures & Functions
Q 1 of 7
Score
/ 7
Click to reveal
TermDefinition
🎯

Mini Test — Procedures & Functions

10 minutes · mixed marks

← 8.2 Arrays Topic 8: Programming Next: 8.4 File Handling →
🔒
Pro Content
Upgrade to access all lessons.
£7.99/month
or £59/year
Subscribe now →