A subroutine is a named block of code that performs a specific task and can be called (executed) multiple times from different points in a program. OCR J277 recognises two types: procedures and functions.
Procedure — performs a task; does not return a value
Function — performs a task AND returns a value using the RETURN keyword
Procedures
A procedure groups instructions under a name so they can be called repeatedly. In OCR J277 pseudocode, procedures are defined with procedure … endprocedure.
// Define a procedure procedure greet() OUTPUT"Hello, welcome to CSZone!" endprocedure
// Call the procedure — can be called multiple times
greet()
greet()
// Procedure with parameters procedure greetUser(name) OUTPUT"Hello, " + name endprocedure
greetUser("Alice")
greetUser("Bob")
Functions
A function works like a procedure but uses return to send a value back to the caller. In OCR J277 pseudocode, functions use function … endfunction.
// Function that returns the square of a number function square(num) return num * num endfunction
result = square(5) // result = 25 OUTPUT result
// Function to calculate area of a rectangle function area(length, width) return length * width endfunction
OUTPUT area(6, 4) // outputs 24
Parameters and Arguments
Term
Meaning
Example
Parameter
Variable named in the subroutine definition
function square(num)
Argument
Actual value passed when the subroutine is called
square(5)
Parameters allow subroutines to work with different data each time they are called, making them reusable and flexible.
Local vs Global Variables
Variables declared inside a subroutine are local — they only exist while the subroutine is running and cannot be accessed outside it. Variables declared in the main program body are global — accessible from anywhere.
global score = 0// accessible everywhere
procedure addPoints(points) local bonus = points * 2// only exists inside here
score = score + bonus endprocedure
addPoints(10) OUTPUT score // 20 OUTPUT bonus // ERROR — bonus is not accessible here
Why Use Subroutines? (Benefits)
Reusability — write code once, call it many times without repeating it
Readability — meaningful names make the main program easier to understand
Maintainability — fix a bug in one place; it's fixed everywhere the subroutine is called
Decomposition — break a large problem into smaller, manageable subproblems
Testing — subroutines can be tested independently (unit testing)
Worked Example — Validation Function
function isValidAge(age) IF age >= 0 AND age <= 120THEN returnTrue ELSE returnFalse END IF endfunction
userAge = int(INPUT) IF isValidAge(userAge) THEN OUTPUT"Valid age" ELSE OUTPUT"Invalid age — please try again" END IF
Exam tip: The key distinction OCR J277 tests: procedures do NOT return a value; functions DO return a value using RETURN. You may be asked to write a function or procedure and the mark scheme will check for this. Also learn: parameters are in the definition, arguments are in the call. Examiners often ask: "State one benefit of using subroutines" — reusability and decomposition are the safest answers.
⚠️ Common Mistakes
Writing a function without a RETURN statement — functions must return a value
Confusing parameters (in the definition) with arguments (in the call)
Trying to use a local variable outside the subroutine where it was declared
Writing "procedure" when you mean "function" — only functions return values
Forgetting endprocedure / endfunction keywords in OCR J277 pseudocode
✅ Notes completed!
▶
Video coming soon
Click slide or press arrow keys to navigate
Worksheet — 2.2.1e Subroutines
8 questions · 22 marks
Q1Define 'subroutine'. State the two types of subroutine in OCR J277 and explain the key difference between them.[3]
✅ Mark scheme
A subroutine is a named block of code that performs a specific task and can be called multiple times [1]. Types: procedure and function [1]. Key difference: a function returns a value (using RETURN); a procedure does not [1].
Q2Distinguish between a parameter and an argument. Give an example of each.[2]
✅ Mark scheme
Parameter: a variable in the subroutine definition, e.g. function square(num) — 'num' is the parameter [1]. Argument: the actual value passed when calling the subroutine, e.g. square(5) — '5' is the argument [1].
Q3Write an OCR J277 pseudocode function called multiply that takes two parameters, a and b, and returns their product. Show a call to this function.[3]
✅ Mark scheme
function multiply(a, b) [1]; return a * b [1]; endfunction; result = multiply(4, 7) or OUTPUT multiply(4,7) [1].
Q4Write an OCR J277 pseudocode procedure called printLine that outputs a dashed line ("----------") 5 times.[3]
✅ Mark scheme
procedure printLine() [1]; FOR i = 1 TO 5; OUTPUT "----------"; NEXT i [1]; endprocedure [1]. Procedure called with printLine().
Q5Explain the difference between a local variable and a global variable. State one advantage of using local variables.[3]
✅ Mark scheme
Local: declared inside a subroutine; only accessible within that subroutine [1]. Global: declared in the main program; accessible from anywhere in the program [1]. Advantage: local variables are destroyed when the subroutine ends (saving memory) / prevent accidental changes to variables in other parts of the program [1].
Q6State three benefits of using subroutines in a program.[3]
✅ Mark scheme
Any 3 from: reusability — code is written once and called multiple times [1]; readability — meaningful names describe what code does [1]; maintainability — bug fixed in one place is fixed everywhere [1]; decomposition — breaks complex problems into smaller subproblems [1]; easier testing — each subroutine tested independently [1].
Q7Write a function isEven(n) that returns True if n is even and False otherwise. Use MOD in your answer.[3]
✅ Mark scheme
function isEven(n) [1]; IF n MOD 2 == 0 THEN return True ELSE return False END IF [1]; endfunction [1]. Accept: return n MOD 2 == 0 for 2 marks if function declaration present.
Q8A student writes a procedure called calcArea that calculates and outputs a rectangle's area. Another student says it should be a function. Who is correct and why?[2]
✅ Mark scheme
The second student is correct [1]: if the area value is needed by other parts of the program (e.g. to use in a formula, store in a variable, or compare with other values), it should be a function that returns the value [1]. A procedure that only outputs is less flexible — the value cannot be used elsewhere.
?
out of 22 — self-mark above
Topic Quiz
Question 1 of 15
You scored
out of 15
Card 1 of 9
Click to reveal definition
🎉
Complete!
Term
Definition
🎯
Mini Test — 2.2.1e Subroutines
10 questions · 10 marks · 10 minutes
⏱ 10:00
10 marks
Section A — Multiple Choice [5 marks]
Q1What is the key difference between a function and a procedure in OCR J277?
Q2In: function add(x, y) — what are x and y called?
Q3Which keyword ends a function definition in OCR J277 pseudocode?
Q4A variable declared inside a subroutine is called:
Q5Which is a benefit of using subroutines?
Section B — Short Answer [5 marks]
Q6Write a function called cube that takes one parameter n and returns n³.
Mark schemefunction cube(n) [1]; return n * n * n (or n^3) [1]; endfunction [½]. Award marks for correct definition, body, and return.
Q7Explain what happens to a local variable when a subroutine finishes executing.
Mark schemeThe local variable is destroyed/deallocated from memory [1]. It can no longer be accessed from outside the subroutine [1]. This means the variable only exists for the duration of the subroutine call.
Q8State the difference between an argument and a parameter. Use the call greet("Mia") and definition procedure greet(name) as your example.
Mark scheme"name" is the parameter — the placeholder variable in the procedure definition [1]. "Mia" is the argument — the actual value passed into the procedure when it is called [1].
Q9Give two reasons why a programmer would use subroutines when writing a large program.
Mark schemeAny 2: avoids repeating code (reusability) [1]; makes code easier to read/understand [1]; makes testing easier — each subroutine tested separately [1]; easier to maintain — a bug is fixed in one place [1]; helps break the problem into smaller parts (decomposition) [1].
Q10Write a procedure called countdown that uses a FOR loop to output numbers from 10 down to 1.
Mark schemeprocedure countdown() [1]; FOR i = 10 TO 1 STEP -1 [1]; OUTPUT i; NEXT i [½]; endprocedure [½]. Award for correct procedure, loop from 10 to 1 with STEP -1, and OUTPUT.