📄 Paper 2 · 4.12 Functional Programming
✓ Free lesson
4.12.1a Functional Programming — Core Concepts
AQA 7517 · A-Level Computer Science · ~15 min read

Functional vs Imperative Programming

AspectImperative (OOP/procedural)Functional
StateVariables change value (mutable state)No mutable state — values are immutable
ControlLoops, if/else, assignment statementsFunction composition, recursion
Side effectsFunctions can modify global statePure functions — no side effects
FocusHOW to do something (sequence of steps)WHAT to compute (describe the result)
ExamplesPython, Java, C++Haskell, F#, Erlang, Clojure

Pure Functions

A pure function has two properties:

  • Deterministic: given the same inputs, always returns the same output
  • No side effects: does not modify any external state (no global variables, no I/O, no database calls)
-- Pure function: always same output for same input double x = x * 2 -- pure -- Impure: depends on/modifies external state count = 0 increment () = count + 1 -- impure: reads external 'count'

Benefits of pure functions: easy to test, easy to reason about, safe to parallelise (no shared state conflicts).

Immutability

In functional programming, data is immutable — once created, it cannot be changed. Instead of modifying a value, a new value is created. This eliminates a whole class of bugs caused by unexpected state changes.

-- Instead of: x = x + 1 (mutation) -- Functional: create new value let y = x + 1 -- x unchanged; y is a new binding

First-Class and Higher-Order Functions

First-class functions: functions are treated as values — they can be:

  • Passed as arguments to other functions
  • Returned as results from functions
  • Stored in variables or data structures

Higher-order functions (HOFs): functions that take one or more functions as arguments, or return a function as a result.

-- apply_twice is a higher-order function apply_twice f x = f (f x) double x = x * 2 apply_twice double 3 -- returns 12

Map, Filter, and Reduce/Fold

Three fundamental higher-order functions in functional programming:

Map

Applies a function to every element of a list, returning a new list of results.

-- map double [1, 2, 3, 4] → [2, 4, 6, 8] map double [1, 2, 3, 4] -- map (\x -> x * x) [1,2,3] → [1, 4, 9]

Filter

Returns a new list containing only the elements that satisfy a predicate (boolean test function).

-- filter even [1,2,3,4,5,6] → [2,4,6] filter even [1, 2, 3, 4, 5, 6] -- filter (>3) [1,2,3,4,5] → [4,5]

Reduce / Fold

Reduces a list to a single value by repeatedly applying a binary function with an accumulator.

-- foldl (+) 0 [1,2,3,4,5] → 15 -- Starts: acc=0; 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15 foldl (+) 0 [1, 2, 3, 4, 5] -- returns 15

Function Composition

Combining two or more functions so the output of one feeds as input to the next. Written mathematically as (f ∘ g)(x) = f(g(x)).

-- In Haskell: the . operator composes functions doubleAndAdd1 = (+1) . (*2) doubleAndAdd1 5 -- 5*2=10, 10+1=11
Exam tip: AQA 7517 requires you to understand and trace map, filter, and fold/reduce with given functions and lists. Be able to: (1) write the result of map/filter/fold applied to a list; (2) explain pure functions and immutability; (3) define first-class and higher-order functions; (4) describe how functional programming differs from imperative. Questions often give pseudocode/Haskell-style notation and ask you to trace or explain.
▶
Click through the slides at your own pace. Use arrow keys or click to advance.
Click slide or press arrow keys to navigate

Worksheet — 4.12.1a Functional Programming

8 questions · instantly marked · AQA 7517 standard

Q1Describe three key differences between functional and imperative programming paradigms.[4]
✅ Mark scheme
Mark scheme
Any 3 of (2 marks each — feature + explanation): Mutable vs immutable state — imperative uses variables that change value; functional uses immutable values [1+1]. Side effects — imperative functions can modify external state (globals, I/O); functional pure functions have no side effects [1+1]. Control flow — imperative uses loops/assignments; functional uses recursion and function composition [1+1]. Focus — imperative describes HOW (sequence of steps); functional describes WHAT (mathematical relationship between inputs and outputs) [1+1].
Q2What is a pure function? Give one advantage of using pure functions.[2]
✅ Mark scheme
Mark scheme
Pure function: always returns the same output for the same input (deterministic) [1]; has no side effects — does not modify external state (no global variables, I/O, or database changes) [1]. Advantage (any 1): easy to test — output is fully predictable [1]; easy to reason about — no hidden dependencies [1]; safe to parallelise — no shared state conflicts [1].
Q3Explain what immutability means in functional programming and explain one benefit it provides.[2]
✅ Mark scheme
Mark scheme
Immutability: once a value is created, it cannot be changed — instead of modifying, a new value is created [1]. Benefit: eliminates bugs caused by unexpected mutation of shared state [1]; makes concurrent programs safer — no risk of one thread modifying data another thread is reading [1].
Q4What is a higher-order function? Give one example.[2]
✅ Mark scheme
Mark scheme
A higher-order function is one that takes one or more functions as arguments, or returns a function as a result [1]. Example: map takes a function and a list as arguments, applies the function to every element, and returns a new list [1]; filter takes a predicate function and a list and returns a filtered list [1]; fold/reduce takes a function, an initial accumulator, and a list [1].
Q5Trace the following: map (\x -> x * x) [1, 2, 3, 4, 5]. State the result and explain each step.[3]
✅ Mark scheme
Mark scheme
map applies the function (\x -> x * x) to each element in the list [1]. 1→1, 2→4, 3→9, 4→16, 5→25 [1 for correct working]. Result: [1, 4, 9, 16, 25] [1]. The original list is unchanged; map returns a NEW list [1].
Q6Trace the following: filter (>4) [1, 3, 5, 7, 2, 6]. State the result.[2]
✅ Mark scheme
Mark scheme
filter applies the predicate (>4) to each element [1]; keeps only elements where the predicate returns True: 5 (5>4 ✓), 7 (7>4 ✓), 6 (6>4 ✓) [1]. Result: [5, 7, 6] [1]. Elements 1, 3, 2 are excluded as they fail the predicate.
Q7Trace the following fold: foldl (+) 0 [10, 20, 30]. Show each accumulator step and give the final result.[3]
✅ Mark scheme
Mark scheme
foldl applies (+) left-to-right with initial accumulator 0 [1]. Step 1: acc=0, element=10 → 0+10=10 [1]. Step 2: acc=10, element=20 → 10+20=30. Step 3: acc=30, element=30 → 30+30=60 [1]. Result: 60 [1].
Q8Explain function composition. Given f(x)=2x and g(x)=x+3, what is (f ∘ g)(5)?[2]
✅ Mark scheme
Mark scheme
Function composition: combining two functions so the output of one is the input of the next [1]; (f ∘ g)(x) = f(g(x)) — g is applied first, then f [1]. Working: g(5) = 5+3 = 8 [1]; f(8) = 2×8 = 16; result: 16 [1].
Functional Programming Quiz
Question 1 of 15
You scored
out of 15
Card 1 of 8
Click to reveal definition
🎉
All cards reviewed!
TermDefinition
🎯

Mini Test — Functional Programming

10 questions · 10 minutes

← 4.11.1 Big Data
69 of 70 · AQA 7517
4.12.1b Functional Programming →