OCR H446, section 1.2.4 (a)
A programming paradigm is a style, or way of thinking, about how to structure a program. Different paradigms suit different kinds of problem. Many languages, such as Python, support more than one.
The need for a variety of paradigms
- No single style suits every problem. A game with many interacting characters, a mathematical calculation and a database query are all easier to express in different ways.
- Different paradigms make programs easier to write, understand, test and maintain for different tasks.
- They affect efficiency, and the way teams divide work.
- They have different strengths: for example, whether a program can easily run in parallel on many processors.
The main paradigms
Paradigms divide into two broad families.
- Imperative: the program is a sequence of instructions that tell the computer how to do a task, changing state (the values of variables). Procedural and object-oriented programming are imperative.
- Declarative: the program says what result is wanted, and leaves the system to work out how. Functional and logic programming, and SQL, are declarative.
Low-level programming
Machine code and assembly language (1.2.4 (c)) give direct control of the hardware, with one instruction at a time. They are used where speed or hardware access matters.
Procedural programming
The program is organised as a set of procedures and functions (subroutines) that work on shared data. It follows sequence, selection and iteration, and is built top-down by breaking a problem into smaller subroutines. See 1.2.4 (b).
- Examples: C, Pascal, early BASIC, and Python used in a simple style.
Object-oriented programming (OOP)
The program is a set of objects that combine data (attributes) and the code that works on it (methods). Objects are created from classes, and interact by sending messages (calling methods). It uses encapsulation, inheritance and polymorphism. See 1.2.4 (e).
- Examples: Java, C#, Python, C++.
Functional programming
The program is built from functions in the mathematical sense: they take inputs and give outputs, with no side effects and no changing of state. Data is immutable, and functions can be passed around and combined. Recursion is used instead of loops.
- Examples: Haskell, Lisp, and the functional features of Python and JavaScript.
- Characteristics: easier to test and reason about, and suited to running in parallel.
Logic programming
The programmer states facts and rules, and asks questions. The system works out the answers by logical reasoning.
parent(alice, bob).
parent(bob, carol).
grandparent(X, Z) :- parent(X, Y), parent(Y, Z).
?- grandparent(alice, Who). % Who = carol
- Example: Prolog. Used in artificial intelligence and expert systems.
Event-driven programming
The program waits for events (a click, a key press, a message) and responds with a handler. It is used for graphical user interfaces and games, and is often combined with an object-oriented style.
Comparison
| Paradigm | Main idea | State changes? | Typical use |
|---|---|---|---|
| Procedural | Sequence of instructions in subroutines | Yes | General programs, system software |
| Object-oriented | Objects with data and methods | Yes (inside objects) | Large applications, games, GUIs |
| Functional | Functions with no side effects | No | Data processing, parallel work |
| Logic | Facts, rules and queries | No | AI and expert systems |
| Low-level | Direct machine instructions | Yes | Drivers, embedded systems |
The same task in different styles
Sum of the squares of the even numbers in a list.
numbers = [1, 2, 3, 4, 5, 6]
# Procedural
total = 0
for n in numbers:
if n % 2 == 0:
total = total + n * n
# Functional
total = sum(n * n for n in numbers if n % 2 == 0)
Both give 56. The procedural version says how (a loop that changes total). The functional version says what (the sum of those squares).
Worked example
Task: suggest a paradigm for each, with a reason.
- A large video game with many characters, each with its own properties and behaviour. Object-oriented: each character is an object with its own data and methods, and inheritance lets similar characters share code.
- A program that does a big calculation over millions of data records, spread across many processors. Functional: functions with no side effects and immutable data can safely run in parallel.
- A device driver that must control hardware directly and use very little memory. Low-level (assembly), or procedural (such as C), for exact control.
Exam tip: give each paradigm's defining characteristic, and justify a choice by linking it to the problem. The key contrast is imperative ("how", changing state) against declarative ("what").
Check yourself
Why are different programming paradigms needed?
No single style suits every problem. Different paradigms make different kinds of program easier to write, understand, test and maintain.
What is the difference between imperative and declarative programming?
Imperative programs give a sequence of instructions that say how to do a task, changing state. Declarative programs state what result is wanted and leave the system to work out how.
Give two characteristics of functional programming
Any two of: functions have no side effects; data is immutable; there is no changing state; functions can be passed as arguments; recursion is used instead of loops.