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.
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.