You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
This lesson covers subroutines — both functions and procedures — as required by OCR J277 Section 2.3. Subroutines are reusable blocks of code that perform a specific task. Understanding how to define, call, and use subroutines with parameters and return values is essential for the OCR GCSE Computer Science exam.
A subroutine is a named block of code that performs a specific task. Instead of writing the same code multiple times, you can write it once as a subroutine and call it whenever needed.
OCR distinguishes between two types of subroutine:
| Type | Returns a value? | OCR Keyword | Use |
|---|---|---|---|
| Function | Yes | function...endfunction | Calculates and returns a result |
| Procedure | No | procedure...endprocedure | Performs an action (e.g. displays output) |
A procedure performs a task but does not return a value. It might display output, modify data, or perform an action.
OCR Pseudocode:
procedure greet(name)
print("Hello, " + name + "!")
print("Welcome to the system.")
endprocedure
// Calling the procedure
greet("Alice")
greet("Bob")
Python:
def greet(name):
print("Hello, " + name + "!")
print("Welcome to the system.")
# Calling the function (Python uses 'def' for both)
greet("Alice")
greet("Bob")
Output:
Hello, Alice!
Welcome to the system.
Hello, Bob!
Welcome to the system.
OCR Exam Tip: In OCR pseudocode, procedures use
procedure...endprocedureand functions usefunction...endfunction. In Python, both usedef. In the exam, use the correct OCR keywords if writing pseudocode.
A function performs a task and returns a value using the return keyword.
OCR Pseudocode:
function calculateArea(length, width)
area = length * width
return area
endfunction
// Calling the function and storing the result
result = calculateArea(5, 3)
print("Area: " + str(result)) // Output: Area: 15
Python:
def calculate_area(length, width):
area = length * width
return area
result = calculate_area(5, 3)
print("Area:", result) # Output: Area: 15
Parameters are the variables listed in the subroutine definition. Arguments are the actual values passed when the subroutine is called.
function add(a, b) // a and b are PARAMETERS
return a + b
endfunction
result = add(5, 3) // 5 and 3 are ARGUMENTS
function calculateBMI(weight, height)
bmi = weight / (height * height)
return bmi
endfunction
myBMI = calculateBMI(70, 1.75)
print("BMI: " + str(myBMI))
def calculate_bmi(weight, height):
bmi = weight / (height * height)
return bmi
my_bmi = calculate_bmi(70, 1.75)
print(f"BMI: {my_bmi:.1f}")
OCR Exam Tip: The distinction between parameters and arguments is a common exam question. Parameters are in the definition; arguments are in the call. Think of parameters as placeholders and arguments as the actual values.
A local variable is declared inside a subroutine and can only be accessed within that subroutine. It is created when the subroutine runs and destroyed when it finishes.
function calculateTax(income)
taxRate = 0.20 // local variable
tax = income * taxRate // local variable
return tax
endfunction
// taxRate and tax cannot be accessed here
A global variable is declared outside all subroutines and can be accessed from anywhere in the program.
global totalScore = 0 // global variable
procedure addScore(points)
totalScore = totalScore + points
endprocedure
addScore(10)
addScore(25)
print(totalScore) // Output: 35
| Reason | Explanation |
|---|---|
| Prevents accidental modification | Other parts of the program cannot change them |
| Avoids naming conflicts | Two subroutines can use the same variable name without interference |
| Makes code easier to debug | Errors are contained within the subroutine |
| Improves reusability | The subroutine is self-contained |
OCR Exam Tip: Using local variables is considered good programming practice. In the exam, if asked "why should local variables be used instead of global variables?", mention: (1) they prevent accidental modification by other parts of the program, and (2) they make the subroutine reusable and easier to debug.
| Benefit | Explanation |
|---|---|
| Code reuse | Write once, call many times — avoids duplicate code |
| Readability | The main program becomes shorter and easier to understand |
| Maintainability | Fix or update code in one place rather than many |
| Testing | Each subroutine can be tested independently |
| Decomposition | Large problems can be broken into smaller, manageable parts |
| Teamwork | Different team members can work on different subroutines |
OCR Pseudocode:
function getGrade(mark)
if mark >= 90 then
return "A*"
elseif mark >= 80 then
return "A"
elseif mark >= 70 then
return "B"
elseif mark >= 60 then
return "C"
else
return "U"
endif
endfunction
procedure displayResult(name, mark)
grade = getGrade(mark)
print(name + ": " + str(mark) + "% = Grade " + grade)
endprocedure
displayResult("Alice", 85)
displayResult("Bob", 62)
Python:
def get_grade(mark):
if mark >= 90:
return "A*"
elif mark >= 80:
return "A"
elif mark >= 70:
return "B"
elif mark >= 60:
return "C"
else:
return "U"
def display_result(name, mark):
grade = get_grade(mark)
print(f"{name}: {mark}% = Grade {grade}")
display_result("Alice", 85)
display_result("Bob", 62)
The flow of control between the main program and the subroutines (call and return) can be visualised:
sequenceDiagram
participant Main as Main program
participant DR as displayResult procedure
participant GG as getGrade function
Main->>DR: call displayResult("Alice", 85)
DR->>GG: call getGrade(85)
GG-->>DR: return "A"
DR->>DR: print "Alice: 85% = Grade A"
DR-->>Main: (procedure ends, no return value)
return.OCR Exam Tip: A common 3-mark question is: "State three benefits of using subroutines." Always give different benefits — do not repeat the same point in different words. Good answers include: code reuse, easier debugging, improved readability, and supporting decomposition.
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.