A Level Computer Science · Topic guide

Programming Paradigms and OOP

A programming paradigm is a fundamental style or approach to structuring a program and expressing computation. Procedural programming organises a program as a sequence of instructions grouped into named procedures and functions, and relies on assignment to change a program's state as it runs. Object-oriented programming (see A1.9) instead bundles data and the behaviour that acts on it together inside interacting objects. Functional programming treats computation as evaluating pure functions that avoid changing any shared state, favouring recursion over loops and often passing functions themselves as values. Declarative programming, including logic programming languages such as Prolog, states WHAT facts and rules are true or what result is wanted, rather than describing HOW to calculate it step by step, leaving an inference engine to work out the answer.

A LevelComponent 2: Algorithms and ProgrammingOCR H446AQAWJECEduqas

Before you start

Make sure you're comfortable with these topics first:

Method

  1. Identify a paradigm by what actually changes as the program runs: procedural and OOP both rely on mutable state (variables/attributes being reassigned), while pure functional code avoids mutable state entirely.
  2. Distinguish procedural from object-oriented by asking where the data lives: procedural code typically passes data between free-standing functions, while OOP bundles data (attributes) and the functions that act on it (methods) together inside an object.
  3. Recognise functional programming by its use of recursion instead of iteration for repetition, and by functions that always return the same output for the same input with no side effects (pure functions).
  4. Recognise a higher-order function (a function that takes another function as a parameter, or returns one) as a distinctly functional feature not normally seen in purely procedural code.
  5. Recognise declarative/logic programming by facts and rules (e.g. Prolog's parent(tom, bob) fact, or a rule built from other facts using :-) that describe relationships that ARE true, queried by asking the language to prove a goal, rather than a sequence of commands to execute.
  6. For an exam question naming a specific language feature or code snippet, match it to its paradigm using these tests, rather than guessing from the language's name alone, since many languages (e.g. Python) support more than one paradigm.
  7. When asked to justify a paradigm choice for a scenario, weigh what the scenario needs: OOP for a system modelled naturally as interacting real-world objects, functional for problems needing predictable, side-effect-free calculations that are easy to test in isolation, declarative for a problem best expressed as constraints or rules to satisfy, procedural for a short, simple, linear task with no benefit from the overhead of the others.

Worked example

The task 'calculate the total of a list of numbers' can be solved in each of the four main programming paradigms. Show a solution in procedural, object-oriented, functional and declarative style, and identify the one feature that makes each solution characteristic of its paradigm.

  1. Procedural: FUNCTION SumList(numbers : ARRAY OF INTEGER) RETURNS INTEGER, total <- 0, FOR i <- 0 TO LEN(numbers) - 1, total <- total + numbers[i], NEXT i, RETURN total, ENDFUNCTION. This is characteristic of procedural programming because it uses a sequence of instructions and a loop that repeatedly mutates a shared variable, total, to build up the answer.
  2. Object-oriented: the same logic is wrapped inside a class, e.g. NumberList, which stores the list as a PRIVATE attribute, items, and exposes a PUBLIC Sum() method that loops over items internally. This is characteristic of OOP because the data (items) and the behaviour that acts on it (Sum()) are bundled together inside one object, rather than a function acting on data passed in from outside.
  3. Functional: FUNCTION SumList(numbers : ARRAY OF INTEGER) RETURNS INTEGER, IF LEN(numbers) = 0 THEN RETURN 0 ELSE RETURN First(numbers) + SumList(Rest(numbers)), ENDFUNCTION. This is characteristic of functional programming because no variable is ever reassigned; instead it uses recursion, breaking the list into its first element and 'the rest', and combines results as each call returns.
  4. Declarative (logic programming, e.g. Prolog): sumlist([], 0). sumlist([H|T], Sum) :- sumlist(T, Rest), Sum is H + Rest. This is characteristic of declarative programming because it states two logical facts/rules about what a list's sum IS, rather than describing the step-by-step procedure for calculating it; the language's own inference engine works out how to apply the rules.
  5. Trace the functional/declarative style on the list [3, 5, 2] to check it agrees with the others: 3 + SumList([5,2]) = 3 + (5 + SumList([2])) = 3 + (5 + (2 + SumList([]))) = 3 + 5 + 2 + 0 = 10.
  6. Final answer: all four versions correctly calculate the total (10 for the list [3, 5, 2]); they differ only in HOW the repetition and state are expressed, which is exactly what distinguishes one programming paradigm from another.

Practice questions

Type your answer and press Check to be marked straight away, or reveal the answer and mark yourself.

Q1State the programming paradigm in which a program is organised as a sequence of instructions grouped into named procedures and functions, relying on assignment to change state.Show answer

Answer: Procedural programming.

Got it right?
Q2State one feature of functional programming that distinguishes it from procedural programming.Show answer

Answer: It avoids mutable state entirely (no variable is ever reassigned once set) and uses recursion instead of loops for repetition, among other features such as pure functions and higher-order functions.

Got it right?
Q3Define what is meant by a 'pure function' in functional programming.Show answer

Answer: A function that always returns the same output for the same input and has no side effects, e.g. it does not change any variable outside itself or perform input/output.

Got it right?
Q4The following Prolog code is given: sport(football). sport(tennis). likes(amir, football). State whether this code describes WHAT is true or HOW to calculate an answer, and name the paradigm this is an example of.Show answer

Answer: It describes WHAT is true, as a set of facts; this is an example of declarative (logic) programming.

Got it right?
Q5Define what is meant by a 'higher-order function'.Show answer

Answer: A function that takes another function as one of its parameters, or that returns a function as its result.

Got it right?
Q6State which paradigm is generally most natural for modelling a simulation of interacting real-world entities, such as vehicles and passengers in a transport simulation, and give one reason.Show answer

Answer: Object-oriented programming; because each entity (e.g. a vehicle, a passenger) can be modelled directly as an object bundling its own data and behaviour, closely matching how the real system is structured.

Got it right?
Q7Explain why Python is often described as a 'multi-paradigm' language.Show answer

Answer: Because Python directly supports writing code in more than one paradigm's style, e.g. plain functions and loops (procedural), classes with inheritance (object-oriented), and functions like map and filter used with lambda expressions (functional), rather than forcing the programmer into only one style.

Got it right?
Q8In Prolog, the rule grandparent(X, Z) :- parent(X, Y), parent(Y, Z). is given, along with the facts parent(tom, bob) and parent(bob, ann). State whether the query grandparent(tom, ann) succeeds, and briefly explain why.Show answer

Answer: Yes, it succeeds; parent(tom, bob) and parent(bob, ann) both hold, so with X=tom, Y=bob and Z=ann, both conditions in the rule are satisfied, making grandparent(tom, ann) true.

Got it right?

Exam-style questions

Written in the style of a A Level Computer Science exam paper, with a full mark scheme.

Q1[2 marks]

State the paradigm being used in each of the following two short extracts: (i) total <- total + price, inside a FOR loop; (ii) RETURN Head(list) + SumList(Tail(list)), with no variable ever reassigned.

Show mark scheme

Tick each line you got. Your score builds from the marks on the scheme.

Nothing ticked yet - 2 available

Got it right?
Q2[4 marks]

A functional-style language provides a higher-order function Map(f, list), which applies function f to every element of list and returns a new list of the results, and a higher-order function Filter(f, list), which returns a new list containing only the elements of list for which f returns TRUE. Using Map and/or Filter, describe how you would produce a new list containing double the value of every even number in a list of integers, without writing a loop or reassigning any variable.

Show mark scheme

Tick each line you got. Your score builds from the marks on the scheme.

Nothing ticked yet - 4 available

Got it right?
Q3[9 marks]

A software company is building a system to validate complex eligibility rules for a benefits scheme, where the rules involve many interacting conditions and exceptions that change frequently as government policy changes. A senior developer suggests using a declarative, rule-based (logic programming) approach instead of the company's usual procedural style. Evaluate this suggestion.

Show mark scheme

Tick each line you got. Your score builds from the marks on the scheme.

Nothing ticked yet - 9 available

Got it right?

See real A Level Computer Science past-paper questions, with official mark schemes

Free printable worksheet

Want more practice on paper? Download the programming paradigms and oop worksheet pack - 13 pages of exam-style questions with a full mark scheme. One email opens every download in this browser for 14 days - no account, no card. Print it for personal and classroom use.

Next topics

Ready to practise programming paradigms and oop? Add it to a printable topic pack for this student in the Pack Builder.

Add to my pack

Not quite what you needed?

Tell us what is missing on programming paradigms and oop, or which topic to write up next. Every request is read, and we reply to every one.

Build a full practice pack.

This topic is one of hundreds in the library - pick the ones a student needs and generate a printable PDF in minutes.