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 the major programming paradigms as required by the OCR A-Level Computer Science (H446) specification, section 2.2. A programming paradigm is a fundamental approach to organising and writing code. You need to understand the characteristics, strengths, and weaknesses of each paradigm and know when to apply them.
A programming paradigm is a style or philosophy of programming that defines how problems are approached and how code is structured. Different paradigms suit different types of problems. Most modern languages support multiple paradigms.
| Paradigm | Core Idea | Example Languages |
|---|---|---|
| Procedural | Step-by-step instructions organised into procedures | C, Pascal, Python |
| Object-Oriented | Programs built around objects that combine data and behaviour | Java, Python, C++ |
| Functional | Programs built from pure functions with no side effects | Haskell, Lisp, Erlang |
| Declarative | Describes what to compute rather than how to compute it | SQL, Prolog, HTML |
Exam Tip: OCR exam questions often ask you to identify a paradigm from a code snippet or to recommend a paradigm for a given scenario. You must justify your answer with specific characteristics.
Procedural programming organises code as a sequence of instructions grouped into procedures (subroutines or functions). The program executes from top to bottom, using control structures such as selection and iteration.
PROCEDURE displayMenu()
OUTPUT "1. Add item"
OUTPUT "2. Remove item"
OUTPUT "3. Quit"
END PROCEDURE
FUNCTION calculateTotal(prices: ARRAY OF REAL) RETURNS REAL
total = 0
FOR i = 0 TO LENGTH(prices) - 1
total = total + prices[i]
NEXT i
RETURN total
END FUNCTION
displayMenu()
Object-Oriented Programming organises programs around objects -- instances of classes -- that encapsulate both data (attributes) and behaviour (methods). OOP models real-world entities and their interactions.
| Feature | Description |
|---|---|
| Encapsulation | Data and methods bundled together; access controlled via access modifiers. |
| Inheritance | New classes reuse and extend existing classes. |
| Polymorphism | Objects of different classes respond to the same method call in different ways. |
| Abstraction | Complex implementation details hidden behind simple interfaces. |
CLASS BankAccount
PRIVATE balance: REAL
PRIVATE accountHolder: STRING
PUBLIC PROCEDURE new(holder: STRING, initialBalance: REAL)
accountHolder = holder
balance = initialBalance
END PROCEDURE
PUBLIC PROCEDURE deposit(amount: REAL)
balance = balance + amount
END PROCEDURE
PUBLIC FUNCTION getBalance() RETURNS REAL
RETURN balance
END FUNCTION
END CLASS
account = NEW BankAccount("Alice", 500.00)
account.deposit(100.00)
OUTPUT account.getBalance() // 600.00
Functional programming treats computation as the evaluation of mathematical functions. It avoids mutable state and side effects, making programs more predictable and easier to reason about.
| Concept | Description |
|---|---|
| Pure functions | Always return the same output for the same input; no side effects. |
| Immutability | Data cannot be changed after creation; new values are created instead. |
| First-class functions | Functions can be assigned to variables, passed as arguments, and returned from other functions. |
| Higher-order functions | Functions that take other functions as parameters or return functions. |
| Recursion | Used instead of loops for iteration. |
| No side effects | Functions do not modify external state. |
# Pure function -- no side effects
def add(a, b):
return a + b
# Impure function -- modifies external state
total = 0
def add_to_total(value):
global total
total += value # Side effect
numbers = [1, 2, 3, 4, 5]
# map: apply a function to every element
squared = list(map(lambda x: x ** 2, numbers)) # [1, 4, 9, 16, 25]
# filter: keep elements satisfying a condition
evens = list(filter(lambda x: x % 2 == 0, numbers)) # [2, 4]
# reduce: combine elements into a single value
from functools import reduce
total = reduce(lambda a, b: a + b, numbers) # 15
Declarative programming focuses on describing what the result should be, rather than specifying how to achieve it step by step.
| Language | Domain |
|---|---|
| SQL | Database queries |
| HTML | Web page structure |
| CSS | Web page styling |
| Prolog | Logic programming |
| Regular expressions | Pattern matching |
-- Declarative: describe WHAT you want
SELECT name, score
FROM students
WHERE score > 80
ORDER BY score DESC;
The equivalent in procedural code would require explicit loops, comparisons, sorting, and a results list.
| Feature | Procedural | OOP | Functional | Declarative |
|---|---|---|---|---|
| Organisation | Procedures | Objects | Functions | Descriptions |
| Data & functions | Separate | Encapsulated | Functions are primary | Implicit |
| State | Mutable variables | Mutable attributes | Immutable | Varies |
| Control flow | Explicit | Method calls | Recursion, HOFs | Implicit |
| Reuse | Procedures | Inheritance, composition | Higher-order functions | Domain-specific |
| Best for | Scripts, small programs | Large systems | Data processing | Databases, markup |
| Scenario | Recommended Paradigm | Reason |
|---|---|---|
| Small utility script | Procedural | Simple, sequential logic |
| Large enterprise application | OOP | Encapsulation, inheritance, maintainability |
| Data transformation pipeline | Functional | Pure functions, easy to parallelise |
| Database queries | Declarative (SQL) | Describes what data is needed |
| GUI application | OOP + Event-driven | Objects model UI components; events drive interaction |
| Scientific computing | Functional | Predictable, no side effects |
Exam Tip: When asked to recommend a paradigm, always explain WHY it is suitable. Simply naming the paradigm without justification will not earn full marks. Link specific characteristics (e.g., encapsulation, immutability) to the scenario requirements.
Most modern languages support multiple paradigms:
| Language | Paradigms |
|---|---|
| Python | Procedural, OOP, Functional |
| JavaScript | Procedural, OOP, Functional, Event-driven |
| Java | OOP (primary), Procedural, Functional (since Java 8) |
| C++ | Procedural, OOP, Functional |
| Haskell | Functional (primary) |
| SQL | Declarative |
Exam Tip: Be prepared to compare paradigms in a table or extended answer. The OCR mark scheme rewards clear comparisons with specific examples and justified recommendations.