SLIDE 1 / 11
CSZone.co.uk
OCR H446 · Component 1 · 1.2.4

Programming Paradigms

OCR A Level Computer Science · cszone.co.uk
H446 SpecA Level
Learning Objectives

By the end of this topic you will be able to:

Define a programming paradigm and explain why different paradigms exist
Describe imperative/procedural programming
Describe declarative programming and give examples
Describe functional programming: pure functions, immutability, higher-order functions, function composition
Paradigms Overview

What is a Programming Paradigm?

A programming paradigm is a fundamental style or approach to programming — a model of computation that shapes how problems are decomposed and solutions structured. Different paradigms suit different types of problems.
Imperative / Procedural
HOW to do it — step-by-step instructions that change program state. C, Python, Pascal.
Declarative
WHAT to achieve — describe the desired result, not the steps. SQL, Prolog, HTML.
Functional
Computation as evaluation of mathematical functions. Haskell, Erlang, parts of Python.
Object-Oriented
Programs as collections of interacting objects. Java, C++, Python. (Covered in 1.2.4e)
Procedural

Imperative / Procedural Paradigm

Programs consist of a sequence of statements that modify program state (variables). Control structures (selection, iteration) and procedures/functions are used to structure the code.
Sequence: statements execute one after another in a defined order, changing the state of variables as the program progresses.
Procedures and functions: named blocks of code that can be called from multiple places, reducing code duplication. Parameters pass data in; return values pass results out.
Global and local state: variables store the current state of the program; side effects occur when functions modify variables outside their local scope.
Most languages students encounter (Python, VB.NET, Pascal) support procedural programming as their primary paradigm.
Declarative

Declarative Programming

In declarative programming, the programmer describes what result is wanted rather than specifying the steps to achieve it. The execution details are handled by the language runtime or inference engine.
SQL: SELECT name FROM students WHERE grade = 'A' — specifies what data is needed; the database engine decides how to retrieve it.
Prolog: a logic programming language. The programmer defines facts and rules; Prolog uses backward chaining to infer whether a query is true. E.g.: parent(tom, bob). parent(bob, ann).
Backtracking in Prolog: if a goal cannot be satisfied, Prolog backtracks to try alternative solutions automatically without the programmer coding this logic.
Functional

Functional Programming

Pure functions: always produce the same output for the same input and have no side effects (no modification of external state). Makes code easier to test and reason about.
Immutability: once a value is bound, it cannot be changed. Instead of modifying a list, a new list is produced. Prevents bugs from unexpected state mutation.
Higher-order functions: functions that take other functions as arguments or return functions. Examples: map (apply function to each element), filter (keep elements satisfying a predicate), reduce/fold (combine elements).
Function composition: combining simple functions to build more complex ones. f ∘ g means apply g first, then f to the result. Encourages modular, reusable code.
Exam Practice
OCR H446 Style · 4 marks
Describe two features of functional programming and explain how each differs from procedural programming.
[4 marks]
2
Pure functions: always return the same output for the same input and produce no side effects. In procedural programming, functions may change global variables (side effects); in functional programming this is avoided, making testing and reasoning simpler.
2
Immutability: values cannot be changed once bound; new values are created instead. In procedural programming, variables are routinely reassigned, which can cause bugs when shared state is modified unexpectedly.
Common Mistakes

Don’t Lose Marks

!
Confusing declarative with functional — both are non-imperative, but declarative means specifying the desired outcome (SQL, Prolog), while functional means computing via pure function evaluation (Haskell). They are different paradigms.
!
Saying functional means using functions in Python — defining a function in Python is procedural. True functional programming requires pure functions with no side effects and immutable data; Python supports a functional style but is not a purely functional language.
!
Forgetting to mention no side effects when describing pure functions — returning the same output for the same input is only half the definition. No modification of external state is equally important and is regularly tested.
1.2.4d Complete
Well done! ✓
Programming Paradigms
Return to lesson to continue