AQA 7517 · A-Level Computer Science · ~15 min read
Functional vs Imperative Programming
Aspect
Imperative (OOP/procedural)
Functional
State
Variables change value (mutable state)
No mutable state — values are immutable
Control
Loops, if/else, assignment statements
Function composition, recursion
Side effects
Functions can modify global state
Pure functions — no side effects
Focus
HOW to do something (sequence of steps)
WHAT to compute (describe the result)
Examples
Python, 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 inputdouble 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 valuelet 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 functionapply_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.
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 functionsdoubleAndAdd1 = (+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]
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!
Term
Definition
🎯
Mini Test — Functional Programming
10 questions · 10 minutes
⏱ 10:00
Section A — Multiple Choice [5 marks]
Q1What is a pure function?
Q2What does map [double] [1,2,3] return? (where double x = x*2)
Q3filter odd [1,2,3,4,5] returns:
Q4A higher-order function is one that:
Q5foldl (+) 0 [1,2,3] evaluates to:
Section B — Short Answer [5 marks]
Q6What is immutability in functional programming?
Mark schemeValues cannot be changed once created; instead of modifying a value, a new value is produced [1].
Q7What does map do in functional programming?
Mark schemeApplies a function to every element in a list and returns a new list containing the results [1].
Q8What does filter do? Give one example.
Mark schemeReturns a new list containing only elements that satisfy a given predicate (boolean function) [1]; e.g. filter even [1,2,3,4] → [2,4] [1].
Q9Give two differences between functional and imperative programming.
Mark schemeAny 2 of: functional uses immutable data vs imperative uses mutable variables; functional uses pure functions with no side effects; functional uses recursion vs imperative uses loops [1 each, max 2].
Q10What is function composition? Give an example using f(x)=x+1 and g(x)=x*2.
Mark schemeCombining two functions so the output of one is the input of the next: (f∘g)(x)=f(g(x)) [1]; example: (f∘g)(3) = f(g(3)) = f(6) = 7 [1].