All subjects · Computer Science · WCP03 Advanced Principles of Computer Science
Functional programming
Edexcel IAL Computer Science · WCP03 Advanced Principles of Computer Science
Functional programming builds programs from functions that behave like mathematical functions: same input, same output, no hidden changes. It is behind modern data processing and parallel computing.
Pure functions and side effects
A pure function always returns the same output for the same input and has no side effects.
A side effect is anything a function does besides returning a value: changing a global variable, changing its argument, printing, writing a file.
def add_tax(price): # pure
return price * 1.2
total = 0
def add_to_total(x): # impure: changes a global
global total
total += xPure functions are easy to test, can be run in any order or in parallel, and their results can be cached.
Immutability and recursion
- Immutable data cannot be changed after it is created. Instead of updating a list, a function returns a new list. This avoids bugs where one part of a program changes data another part relies on.
- Because loop counters change (mutate), functional languages use recursion instead of loops: a function calls itself on a smaller problem until it reaches a base case.
def total(xs):
if xs == []: # base case
return 0
return xs[0] + total(xs[1:])First-class and higher-order functions
- Functions are first-class: they can be stored in variables, passed as arguments and returned as results, just like numbers.
- A higher-order function takes a function as an argument or returns one. The three classics:
- map(f, list): applies f to every item.
- filter(p, list): keeps the items for which p is true.
- reduce(f, list): combines the items into one value.
from functools import reduce
xs = [3, 8, 5, 12, 7]
list(map(lambda x: x * 2, xs)) # [6, 16, 10, 24, 14]
list(filter(lambda x: x > 5, xs)) # [8, 12, 7]
reduce(lambda a, b: a + b, xs) # 35Benefits and drawbacks
Benefits: fewer bugs from shared changing state; easier testing and reasoning; natural fit for parallel processing (no shared mutable data to protect); concise code.
Drawbacks: unfamiliar style; deep recursion can use a lot of memory (stack overflow); copying data instead of changing it can be slower; real programs need side effects (input, output, files), which must be carefully managed.
Worked example
xs = [3, 8, 5, 12, 7]. Find reduce(lambda a, b: a + b, map(lambda x: x * x, filter(lambda x: x % 2 == 1, xs))).
- filter keeps odd numbers: [3, 5, 7]
- map squares them: [9, 25, 49]
- reduce adds them: 83
Answer: 83
Check yourself
- Which function is pure?
- def sq(x): return x * x
- def log(x): print(x); return x
- def inc(): global n; n += 1
- def add(xs): xs.append(1); return xs
Show answer
def sq(x): return x * x. sq depends only on x and changes nothing else.
- What is a side effect?
- The value a function returns
- An error in the syntax
- A change a function makes outside returning its value, such as altering a global variable
- A parameter passed to a function
Show answer
A change a function makes outside returning its value, such as altering a global variable. Pure functions have none.
- What is a higher-order function?
- A function that takes a function as an argument or returns a function
- A function that calls itself
- A function with more than three parameters
- A function stored in a class
Show answer
A function that takes a function as an argument or returns a function. map, filter and reduce are higher-order.
- What does list(map(lambda x: x + 1, [3, 8, 5, 12, 7])) return? Type the numbers separated by commas.
Show answer
4, 9, 6, 13, 8. Each item gets 1 added: [4, 9, 6, 13, 8].
- How many items does list(filter(lambda x: x > 6, [3, 8, 5, 12, 7])) return?
Show answer
3. Items greater than 6: [8, 12, 7].
- What does reduce(lambda a, b: a * b, [2, 3, 4]) return?
Show answer
24. (2 x 3) x 4 = 24.
- Why does functional programming favour immutable data?
- Immutable data uses no memory
- Data that cannot change cannot be altered unexpectedly by another part of the program
- It makes recursion impossible
- It removes the need for functions
Show answer
Data that cannot change cannot be altered unexpectedly by another part of the program. Avoiding shared changing state removes a whole class of bugs.
- Why is functional code well suited to parallel processing?
- Pure functions share no changing state, so they can run at the same time safely
- Functional languages only run on GPUs
- Recursion always runs in parallel
- Side effects make code faster
Show answer
Pure functions share no changing state, so they can run at the same time safely. No shared mutable data means no clashes between threads.
Sources
- Pearson Edexcel International Advanced Subsidiary and Advanced Level in Computer Science specification (2026), Issue 1
- Pearson Edexcel IAL Computer Science sample assessment materials (Unit 3 paper, resource booklet and mark scheme)
Last updated 8 November 2026. Spotted a mistake? Tell us.