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.

Practise this in the app: instant marking and a fix for every mistake. Free, no email needed to start.

Practise this lesson

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 += x

Pure functions are easy to test, can be run in any order or in parallel, and their results can be cached.

Immutability and recursion

def total(xs):
    if xs == []:            # base case
        return 0
    return xs[0] + total(xs[1:])

First-class and higher-order functions

- 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)        # 35

Benefits 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))).

  1. filter keeps odd numbers: [3, 5, 7]
  2. map squares them: [9, 25, 49]
  3. reduce adds them: 83

Answer: 83

Key idea: Pure functions: same input, same output, no side effects. Immutable data; recursion instead of loops. First-class functions; higher-order functions map, filter, reduce.

Check yourself

  1. 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.

  2. 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.

  3. 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.

  4. 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].

  5. 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].

  6. What does reduce(lambda a, b: a * b, [2, 3, 4]) return?
    Show answer

    24. (2 x 3) x 4 = 24.

  7. 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.

  8. 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.

Practise with instant feedback

Sources

Last updated 8 November 2026. Spotted a mistake? Tell us.