Modularity, functions and procedures, parameter passing by value and by reference. (2.2.1)
- 20p13280
- Oct 21, 2025
- 4 min read
This post covers the following from the specification (2.2.1):
(a) Programming constructs: sequence, iteration, branching.
(b) Recursion, how it can be used and compares to an iterative approach. (Skipped for now)
(c) Global and local variables.
(d) Modularity, functions and procedures, parameter passing by value and by reference.
(e) Use of an IDE to develop/debug a program.
(f) Use of object oriented techniques. (Skipped for now)
Modularity
Modularity is where a complex program is broken down into smaller simpler subprograms/problems, like decomposition. The process of breaking down tasks into smaller tasks repeats until you can't break them down any further. Each module is self-contained which makes it easy to change them out without impacting or having to re-write the entire program, a good example of this is the networking stack with networking layers. If you were to change out the Transport layer for example, the rest of the layers are self-contained modules and as long as the correct protocol is used there will be no impact to the other modules. This principle is what makes up modularity.
This benefits the programmer and other working on the same program as it improves the readability of the program so it's easier for other programmers to understand the code. It's easier to debug as an error can only come from 1 module instead of some complex code within the program. As mentioned earlier it's easier to update the modules since they are individual and self-contained. Modularity also makes it simpler and easier for programmers to copy other subroutines and modules from one program to the other since they are all self-contained.
Subroutines
A subroutine is a block of re-usable code that a programmer can write one and assign an identifier to. This code can then be called anywhere within a program and it will execute the code inside of the block. This saves the programmer time from having to constantly copy out code that they've already written and this is incredibly useful in something like a loop.
Subroutines are also a great tool for making code more readable and easy to understand, this is because you can use decomposition to break big problems down into smaller tasks that can be handled by subroutines, so something that might have been really advanced can be shown in a few steps by calling subroutines, the subroutines would have been made with the code that origionally did the complex advanced task.
In Python a subroutine is defined using the def keyword and an simple subroutine can look like these:
def hello_world():
print("Hello, World.")
def reverse_string(input):
return input[::-1]There are different types of subroutines, both of which have been shown in the example above. They are proceedures and functions
Procedures
A procedure is a type of subroutine that doensn't return a value. An example of a basic prodecure is the hello_world defined in the example above. In python they are still defined using the def keyword but in OCR Exam Reference Language/Pseudocode they are defined using the proceedure keyword.
Functions
A function is the opposite of a procedure, a function will return a value. This means that you can call a function and set a variable to the result of it or do something with the value it returns. An example of a function is reverse_string in the example above. In python they are still defined using the def keyword but in OCR Exam Reference Langauge/Pseudocode they are defined using the function keyword.
For a function to be a function, it needs a "return" keyword. This is important as you put the data you want the function to return after the keyword. If you don't have the return keyword it would just be a procedure, or if some languages have checks it may throw an error as nothing is returned.
Parameter passing
A parameter is a value that a subroutine can expect to be passed in when it is run. These are useful as it allows programmers to pass data that was otherwise unaccessible to the subroutine. This allows for better written code in terms of maintainability and readability. When a value is passed into a parameter during the subroutine call, the variable passed in is called an argument. The word argument adn parameter are NOT interchangable. An example of a subroutine being called with parameters and arguments is as followed:
def add(num1,num2):
added = num1+num2
return added
print(f"The sum of 2 and 1 is {add(2,1)}")In this example there is a function called add which takes in 2 parameters, num1 & num2. These are then used as local variables within the function when they are added and returned. On the print line, an f string is used to allow the function to be called and have its result placed into the string directly. Within the line the add function is called with the arguments of 2 and 1. Since the first argument is 2 and the first parameter specified is num1, this means that 2 will be assigned to num1 and the same happens for the second argument becomming the second parameter.
There is two ways to pass arguments into a function, these are Passing by reference and passing by value.
By reference
When passing by reference, instead of passing in the value of the variable, the memory address of the variable is passed in. This means that within the subroutine if the value of the variable updates, it updates everywhere not just within a local scope.
By value
Passing by value means creating a copy of the argument variable thats passed in instead of passing in the memory address. This means that within the subroutine if the value of the (local) variable updates, it only updates in the subroutine and doesn't impact anywhere else. This approach is more resilient to logic errors where you accidentally change a variables value.


Comments