AQA 7517, section 4.1.2.1
A programming paradigm is a style, or way of thinking, about how to structure a program. Different paradigms organise the same code in different ways. The specification asks you to understand two of them, procedural and object-oriented, and to have programmed in each.
Procedural programming
In the procedural paradigm, a program is a list of instructions organised into subroutines (procedures and functions). The data and the code that works on it are kept separate: data is passed to subroutines as parameters, and results come back.
Key characteristics:
- The program is built from sequence, selection and iteration.
- The problem is broken down into subroutines, each doing one job.
- Data is held in variables and data structures, and subroutines operate on it.
- The focus is on what the program does, step by step.
def calculate_area(width, height):
return width * height
def show_area(width, height):
print("Area:", calculate_area(width, height))
show_area(4, 3)
Object-oriented programming (OOP)
In the object-oriented paradigm, a program is a set of objects that work together. Each object bundles its own data (attributes) with the code (methods) that works on that data. Objects are created from classes.
Key characteristics:
- The program is organised around objects, which model things in the problem (a customer, a bank account, a bullet).
- Data and methods are kept together in a class.
- Objects keep their data private and give controlled access to it (encapsulation).
- Classes can inherit from each other, so code is reused.
- The focus is on the things the program is about, and what they can do.
class Rectangle:
def __init__(self, width, height):
self.width = width
self.height = height
def area(self):
return self.width * self.height
r = Rectangle(4, 3)
print(r.area()) # 12
These ideas are covered in detail in 4.1.2.3.
Comparing the two
| Procedural | Object-oriented | |
|---|---|---|
| Program is built from | Subroutines | Classes and objects |
| Data and code | Kept separate | Kept together in objects |
| Data access | Usually open to any subroutine | Often hidden and accessed through methods |
| Code reuse | By calling subroutines | By subroutines, inheritance and composition |
| Best suited to | Small to medium programs, straightforward tasks | Large programs modelling many interacting things |
| Examples of languages | C, early BASIC, Pascal | Java, C#, Python (also supports OOP) |
Many languages, including Python, support both, and real programs often mix the two.
Worked example
Task: describe how a simple "bank account" program would be built in each paradigm.
Procedural: you'd have a variable for the balance, and subroutines such as deposit(balance, amount) and withdraw(balance, amount) that take it as a parameter and return the new balance. The data and the subroutines are separate, so nothing stops other code from changing the balance directly.
Object-oriented: you'd create a BankAccount class with a private balance attribute and methods deposit and withdraw. The balance can only be changed through these methods, which can check, for example, that you aren't withdrawing more than is available. Each account is an object, so you can create thousands, each with its own balance.
Exam tip: if you're asked to compare the paradigms, don't just list features of each. Contrast them directly, such as "in procedural programming the data is separate from the subroutines, whereas in OOP the data and methods are combined in an object".
Check yourself
What is a programming paradigm?
A style or approach to organising and writing programs.
What is the main building block of a procedural program, and of an OOP program?
Subroutines (procedures and functions) in a procedural program, and classes and objects in an object-oriented program.
Give one difference between how procedural and object-oriented programs treat data
In a procedural program the data is separate from the subroutines that use it. In an object-oriented program the data and the methods that act on it are combined inside objects, and the data is usually hidden.