A programming paradigm is a fundamental style or approach to computer programming. It defines how tasks are structured, how data is managed, and how the computer executes instructions. Different paradigms offer different ways of thinking about and solving problems.
Choosing the right paradigm affects:
The imperative paradigm focuses on how to achieve a result. Programs consist of explicit step-by-step commands that change the program's state.
Think of it as a recipe: do step 1, then step 2, then step 3.
The declarative paradigm focuses on what the result should be, without describing the control flow.
| Feature | Imperative | Declarative |
|---|---|---|
| Focus | How to do it | What to do |
| Control flow | Explicit | Hidden/abstracted |
| Example | C, Python loops | SQL, HTML |
The procedural paradigm is a sub-type of the imperative paradigm. It organises code into reusable procedures (also called functions or subroutines).
Key characteristics:
// Example: Procedural approach in C
void greet() {
printf("Hello, World!\n");
}
int main() {
greet(); // calling a procedure
return 0;
}
The Object-Oriented paradigm organises programs around objects — entities that bundle data (attributes) and behaviour (methods) together.
Core concepts:
| Concept | Description |
|---|---|
| Class | A blueprint/template for creating objects |
| Object | An instance of a class |
| Encapsulation | Hiding internal data; exposing only what is needed |
| Inheritance | A new class (derived) acquires properties of an existing class (base) |
| Polymorphism | The same interface can be used for different underlying data types |
| Abstraction | Hiding complex implementation details |
Examples: Python, Java, C++.
# Example: OOP in Python
class Animal:
def __init__(self, name):
self.name = name # attribute
def speak(self): # method
pass
class Dog(Animal): # Inheritance
def speak(self):
return "Woof!"
dog = Dog("Rex")
print(dog.speak()) # Output: Woof!
The functional paradigm treats computation as the evaluation of mathematical functions. It emphasises:
Examples: Haskell, Lisp, Erlang. Python also supports functional features (map, filter, lambda).
# Functional style in Python
numbers = [1, 2, 3, 4, 5]
squares = list(map(lambda x: x**2, numbers))
print(squares) # Output: [1, 4, 9, 16, 25]
The logic paradigm expresses programs as a set of logical rules and facts. The system uses inference to derive conclusions.
% Prolog example
parent(tom, bob).
parent(bob, ann).
grandparent(X, Z) :- parent(X, Y), parent(Y, Z).
| Paradigm | Focus | Key Feature | Example Languages |
|---|---|---|---|
| Imperative | How | State changes | C, Pascal |
| Declarative | What | Abstracted control | SQL, HTML |
| Procedural | How (modular) | Procedures/functions | C, FORTRAN |
| Object-Oriented | Objects | Encapsulation, Inheritance | Python, Java, C++ |
| Functional | Functions | Pure functions, immutability | Haskell, Lisp |
| Logic | Rules/Facts | Inference engine | Prolog |