Skip to content
Definition
Modular programming is the practice of breaking down a large program into smaller, manageable modules. Each module is a self-contained block of code designed to perform a specific task.

Top-Down Design

Key Point
Top-Down Design:
• Start with highest-level problem
• Break down into smaller sub-problems
• Each sub-problem handled by a module
• Makes complex problems manageable
• Allows team collaboration

Functions vs Procedures

Key Point
Routine typeReturn behaviourTypical use
FunctionReturns a valueCalculations / expressions (e.g. average = calcMean(scores))
ProcedureUsually no direct return valuePerforming actions/side effects (e.g. print, write, update)
Worked Example
Parameter Passing
pseudocode
// PASS BY VALUE: subroutine gets a copy
procedure doubleByValue(x):
    x = x * 2
    print(x)      // Prints 20
end procedure

number = 10
doubleByValue(number)
print(number)     // Still 10! Original unchanged

// PASS BY REFERENCE: subroutine can modify original
procedure doubleByReference(ref x):
    x = x * 2
    print(x)      // Prints 20
end procedure

number = 10
doubleByReference(number)
print(number)     // Now 20! Original was modified
When to use pass by reference:
• Need to return multiple values
• Modifying large data structures (avoid copying)
• When the subroutine must change the original

Passing Parameters by Value and by Reference

Definition
Passing by value gives the subroutine a copy of the data. Changes made inside the subroutine affect only the copy, which is discarded when the subroutine ends, so the original variable is unchanged.
Definition
Passing by reference gives the subroutine the memory address of the original variable. Changes made inside the subroutine are made to the original and are kept after the subroutine ends.
FeatureBy valueBy reference
What is passedA copy of the valueThe address of the variable
Effect on the originalNoneChanged by the subroutine
MemoryExtra memory is needed for the copyNothing is copied, which matters for a large array
Typical useData the subroutine only needs to readData the subroutine must update, such as an array to be sorted
Worked Example
The byVal and byRef Keywords

OCR pseudocode states how each parameter is passed by writing byVal or byRef after its name.
pseudocode
procedure update(score:byVal, total:byRef):
    score = score + 10
    total = total + score
end procedure

points = 5
sum = 100
update(points, sum)
print(points)    // 5: only the copy was changed
print(sum)       // 115: the original was changed
Inside the procedure score becomes 15 and total becomes 100 + 15 = 115. When the procedure ends, points is still 5 because it was passed by value, and sum is 115 because it was passed by reference.