OCR H446, section 2.2.1 (d)
Modularity
Modularity means dividing a program into separate, self-contained parts (modules), each with one job (2.1.3 (d)).
- Each module can be written, tested and fixed on its own.
- Teams can build modules at the same time.
- Modules can be reused, and replaced without affecting the rest.
- The program is easier to read and maintain.
A module may be a subroutine, or a file of related subroutines (a library) that is imported.
import math # a module of maths subroutines
print(math.sqrt(16))
Functions and procedures
Both are named subroutines. You define one once and call it whenever it is needed.
| Function | Procedure | |
|---|---|---|
| Returns a value | Yes | No |
| Called | In an expression | As a statement |
| Use for | Calculating and giving back a result | Doing an action (printing, updating) |
def area(width, height): # function: returns a value
return width * height
def show_area(width, height): # procedure: does something
print("Area:", area(width, height))
a = area(3, 4) # 12
show_area(3, 4)
OCR pseudocode makes the distinction explicit:
function area(width, height)
return width * height
endfunction
procedure showArea(width, height)
print("Area: " + str(area(width, height)))
endprocedure
(Python uses def for both, and a procedure simply doesn't return a value.)
Parameters and arguments
- A parameter is the name in the definition (
width,height). - An argument is the actual value passed when it is called (
3,4). - Parameters are local variables (2.2.1 (c)).
Passing parameters: by value and by reference
In OCR pseudocode, the passing mode is written after the parameter name: procedure foobar(x:byVal, y:byRef). Unless a question says otherwise, parameters are passed by value.
By value
A copy of the argument's value is given to the subroutine. Changes made inside don't affect the original.
procedure addOne(n:byVal)
n = n + 1
endprocedure
x = 5
addOne(x)
print(x) // still 5
By reference
The subroutine is given the memory location (a reference) of the original, so it works on the original itself. Changes made inside do affect it.
procedure addOne(n:byRef)
n = n + 1
endprocedure
x = 5
addOne(x)
print(x) // now 6
| By value | By reference | |
|---|---|---|
| What is passed | A copy of the value | The address of the variable |
| Original changed? | No | Yes |
| Safe from side effects? | Yes | Less so |
| Memory | A copy is made (costly for large data) | No copy (efficient for large data) |
| Use when | Subroutine only needs to read | Subroutine must change the original, or return several values |
By reference is useful for a swap subroutine, or one that must modify a large array without copying it.
procedure swap(a:byRef, b:byRef)
temp = a
a = b
b = temp
endprocedure
What Python does
Python passes a reference to the object. What happens then depends on whether the object can be changed (mutable):
def add_one(n):
n = n + 1 # makes a NEW integer (caller's unchanged)
def add_item(items):
items.append(99) # changes the SAME list the caller has
x = 5
add_one(x)
print(x) # 5
nums = [1, 2]
add_item(nums)
print(nums) # [1, 2, 99]
Numbers, strings and tuples (immutable) behave as if passed by value. Lists and dictionaries (mutable) behave as if passed by reference. In an exam, use the byVal and byRef ideas from the pseudocode.
Worked example
Task: write a procedure that takes a list of marks and adds 5 to every mark, capped at 100. Say whether the list should be passed by value or by reference, and why.
def add_bonus(marks):
for i in range(len(marks)):
marks[i] = min(marks[i] + 5, 100)
scores = [60, 98, 45]
add_bonus(scores)
print(scores) # [65, 100, 50]
- By reference, because the procedure must change the original list. If it were passed by value, the changes would be made on a copy that is then thrown away, and
scoreswouldn't change. - It is also more efficient: a long list isn't copied.
- The disadvantage is a side effect: the caller's data is changed, so the documentation should say so.
Exam tip: by value means a copy (original unchanged). By reference means the address (original can change). When asked which to use, link it to whether the original needs changing and to efficiency for large data.
Check yourself
What is the difference between a function and a procedure?
A function returns a value. A procedure carries out actions but doesn't return a value.
What is the difference between a parameter and an argument?
A parameter is the name used in the subroutine definition. An argument is the actual value given to it when it's called.
What is the effect of passing a variable by value?
The subroutine gets a copy, so changes made inside it don't affect the original variable.
Give one advantage of passing by reference
The original can be changed by the subroutine, and large data isn't copied, which saves memory and time.
Give two benefits of modular programming
Any two of: parts can be tested separately; teams can work in parallel; modules can be reused; easier to read and maintain; one part can be replaced without affecting others.