Subroutines: procedures and functions
A subroutine is a named block of code that performs a specific task and can be called (invoked) from multiple places in a program. OCR J277 Paper 2 tests writing, calling and tracing subroutines, distinguishing procedures from functions, and explaining the benefits of subroutines.
Types of subroutine
Procedure
- Performs a task but does not return a value.
- Called by using its name; execution returns to the calling code when the procedure ends.
PROCEDURE greet(name)
OUTPUT "Hello, " + name + "!"
END PROCEDURE
// Calling the procedure
greet("Alice") // outputs: Hello, Alice!
greet("Bob") // outputs: Hello, Bob!
Function
- Performs a task and returns a value to the calling code.
- Called within an expression or assignment.
FUNCTION square(n)
RETURN n * n
END FUNCTION
result = square(5) // result = 25
OUTPUT square(3) // outputs: 9
Parameters and arguments
- A parameter is a variable in the subroutine definition that receives a value when the subroutine is called.
- An argument is the actual value passed to the subroutine when it is called.
FUNCTION add(a, b) // a and b are parameters
RETURN a + b
END FUNCTION
total = add(10, 20) // 10 and 20 are arguments
Multiple parameters are separated by commas. Parameters make subroutines reusable — the same code can operate on different data each time it is called.
Local vs global variables
Local variables
- Declared inside a subroutine.
- Only accessible within that subroutine.
- Created when the subroutine is called; destroyed when it finishes.
Global variables
- Declared outside all subroutines (at the program level).
- Accessible from anywhere in the program.
- Persist for the entire program's lifetime.
Why prefer local variables?
- Encapsulation: subroutines work independently without depending on external state.
- Avoids naming conflicts: two subroutines can each have a local variable called i without conflict.
- Easier to debug: if a variable has a wrong value, only the code inside that subroutine could have caused it.
x = 10 // global variable
FUNCTION calculate(n)
result = n * 2 // local variable
RETURN result
END FUNCTION
OUTPUT calculate(x) // outputs: 20
// 'result' does not exist here — it was local to calculate()
Benefits of using subroutines
- Reusability: write once, call many times — avoids code duplication.
- Decomposition: breaks a large program into manageable, single-purpose blocks.
- Easier testing: each subroutine can be tested independently.
- Easier maintenance: changing one subroutine updates all places that call it.
- Readability: well-named subroutines make code self-documenting.
Common OCR exam mistakes
- Confusing procedure and function — a procedure does NOT return a value; a function DOES.
- Trying to use a local variable outside its subroutine — local variables do not exist beyond the subroutine.
- Forgetting that parameters are local to the subroutine — they cannot be accessed by the calling code.
- Calling a function but not capturing or outputting the return value — the result is lost.
AI-generated · claude-opus-4-7 · v3-ocr-computer-science