A programming paradigm is a style or approach to programming. Different paradigms suit different problems. OCR H446 requires knowledge of four key paradigms:
| Paradigm | Core idea | Examples |
|---|---|---|
| Procedural / Imperative | Step-by-step instructions; sequence, selection, iteration | Python (basic), C, Pascal |
| Object-Oriented (OOP) | Objects with data and behaviour; encapsulation, inheritance, polymorphism | Python (OOP), Java, C++ |
| Functional | Functions as first-class objects; avoid state and mutable data; higher-order functions | Haskell, Erlang; Python supports some features |
| Declarative | Describe WHAT is wanted, not HOW; the runtime/engine works it out | SQL, HTML, Prolog |
OOP organises code around objects — instances of classes that bundle together data (attributes) and behaviour (methods). The four pillars of OOP are:
Bundling data (attributes) and the methods that operate on it into a single class, and hiding internal implementation by making attributes private. Only the public interface (methods) is exposed. This prevents accidental modification of internal state.
A child class (subclass) inherits attributes and methods from a parent class (superclass). The child can override methods or add new ones. Promotes code reuse and represents an IS-A relationship (a Dog IS-A Animal).
Different classes respond to the same method name in different ways. Example: Animal.makeSound() could produce "Woof" for a Dog and "Meow" for a Cat. The calling code doesn't need to know the specific type — it just calls makeSound().
Hiding implementation detail behind a public interface. Users of a class interact through its methods without needing to understand the internal code. Related to, but distinct from, abstraction as a computational thinking technique.
A class is a blueprint/template. An object is an instance of a class. A class defines attributes (data) and methods (behaviour). The __init__ constructor sets up initial attribute values when an object is created.
class Animal:
def __init__(self, name, sound):
self.__name = name # private attribute
self.__sound = sound
def speak(self): # public method
return self.__name + " says " + self.__sound
dog = Animal("Dog", "Woof")
print(dog.speak()) # Dog says Woof
class Shape:
def __init__(self, colour):
self.colour = colour
def area(self):
return 0
class Circle(Shape):
def __init__(self, colour, radius):
super().__init__(colour)
self.radius = radius
def area(self): # override parent method
return 3.14159 * self.radius ** 2
Functional programming treats computation as the evaluation of mathematical functions. Key features:
map(), filter(), reduce().# Higher-order functions in Python
nums = [1, 2, 3, 4, 5, 6]
evens = list(filter(lambda x: x % 2 == 0, nums)) # [2, 4, 6]
doubled = list(map(lambda x: x * 2, nums)) # [2, 4, 6, 8, 10, 12]
# Function passed as argument
def apply(func, value):
return func(value)
print(apply(lambda x: x**2, 5)) # 25
Recursion is when a function calls itself. It is a key technique in both functional and procedural programming. Every recursive function needs:
def factorial(n):
if n == 0: # base case
return 1
return n * factorial(n - 1) # recursive case
# factorial(5) → 5 * factorial(4) → 5 * 4 * factorial(3) → ...
# → 5 * 4 * 3 * 2 * 1 = 120
Each recursive call adds a new stack frame. Deep recursion can cause a stack overflow if the call stack runs out of memory. Tail recursion (where the recursive call is the last operation) can be optimised by some languages to avoid this.
The most fundamental paradigm. Code is a sequence of instructions. The key constructs are: sequence, selection (if/else), iteration (while/for), and sub-routines (functions/procedures). Variables, parameters, return values, and local/global scope are all part of procedural thinking.
| Local variable | Global variable | |
|---|---|---|
| Scope | Inside the function/block where declared | Entire program |
| Lifetime | Created when function called, destroyed when it returns | Exists for entire program run |
| Access | Only by the owning function | Any function (can read; writing needs global keyword in Python) |
| Side effects | None — cannot affect other functions | Risk of unintended modification from any function |
| Recommendation | Preferred — safer, more maintainable | Avoid where possible — use parameters/return values instead |
8 questions · 24 marks · instantly marked
| Term | Definition |
|---|