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.
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.
PROCEDURE greet(name : STRING)
OUTPUT "Hello, " & name
ENDPROCEDURE
CALL greet("Alice") // Outputs: Hello, Alice
CALL greet("Bob") // Outputs: Hello, Bob
PROCEDURE drawLine()
OUTPUT "-------------------"
ENDPROCEDURE
CALL drawLine()
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.
FUNCTION square(n : INTEGER) RETURNS INTEGER
RETURN n * n
ENDFUNCTION
result ← square(5) // result = 25 OUTPUT square(4) // Outputs 16 OUTPUT square(3) + 1 // Outputs 10
FUNCTION isEven(num : INTEGER) RETURNS BOOLEAN
IF num MOD 2 = 0 THEN
RETURN TRUE
ELSE
RETURN FALSE
ENDIF
ENDFUNCTION
| Feature | Procedure | Function |
|---|---|---|
| Returns a value? | No | Yes (using RETURN) |
| Return type in header? | No | Yes — RETURNS datatype |
| Called with? | CALL procedureName() | Used in an expression or assignment |
| ENDPROCEDURE / ENDFUNCTION | ENDPROCEDURE | ENDFUNCTION |
| Typical use | Printing, modifying global data | Calculating and returning a result |
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
4 questions · 11 marks
| Term | Definition |
|---|
10 minutes · mixed marks