🔒
Unlock Everything
£7.99/month
or £59/year
Subscribe now →
💻 Component 2 · 2.2 Programming
2.2.1e Subroutines (Functions & Procedures)
OCR J277 · GCSE Computer Science · ~12 min read
Notes
Video
Slides
Worksheet
Quiz

What is a Subroutine?

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 procedureendprocedure.

// 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 functionendfunction.

// 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

TermMeaningExample
ParameterVariable named in the subroutine definitionfunction square(num)
ArgumentActual value passed when the subroutine is calledsquare(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 <= 120 THEN
        return True
    ELSE
        return False
    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!
TermDefinition
🎯

Mini Test — 2.2.1e Subroutines

10 questions · 10 marks · 10 minutes

← 2.2.1d Arrays 2.2 Programming 2.2.1f File Handling →