Functional programming builds programs by combining functions, in the mathematical sense, rather than by changing variables step by step. It is a different way of thinking from procedural code.

Core ideas

  • Pure functions: a function’s output depends only on its inputs, and it has no side effects (no changing global variables, no printing, no file writes). Calling Square(4) always returns 16.
  • Immutability: data is not changed after creation. Instead of modifying a list you produce a new list.
  • First-class functions: functions can be stored in variables, passed as arguments and returned as results.
  • Higher-order functions: functions that take other functions as parameters or return them.
  • No loops in the pure style: repetition is done with recursion and with higher-order functions.

Function application and composition

Applying a function means giving it an argument: double(5) gives 10. Composition chains functions so that the output of one becomes the input of the next: (f . g)(x) = f(g(x)). If double(x) = x * 2 and inc(x) = x + 1, then double(inc(3)) = 8.

Standard higher-order functions

The Cambridge syllabus names three:

  • map: apply a function to every item of a list.
  • filter: keep only the items for which a test function returns true.
  • fold (reduce): combine all the items into a single value using a function.
map(double, [1, 2, 3])        -> [2, 4, 6]
filter(isEven, [1, 2, 3, 4])  -> [2, 4]
fold(add, 0, [1, 2, 3, 4])    -> 10

The same ideas exist in Python:

nums = [1, 2, 3, 4]
list(map(lambda x: x * 2, nums))        # [2, 4, 6, 8]
list(filter(lambda x: x % 2 == 0, nums)) # [2, 4]
from functools import reduce
reduce(lambda a, b: a + b, nums, 0)      # 10

Lists are often handled as a head (first item) and a tail (the rest). A recursive sum is: sum of an empty list is 0, otherwise head plus sum of tail.

Advantages and drawbacks

  • Pure functions are easy to test, because the same inputs always give the same output.
  • No hidden changes to shared data, which makes parallel processing safer.
  • Programs are often short and clear.
  • Drawbacks: input and output are awkward in a pure model, recursion can use more memory, and the style takes practice. Languages include Haskell, Lisp and F#, while Python and JavaScript support the style too.

Check your understanding

What is a pure function?

  • One whose output depends only on its inputs and has no side effects
  • One that prints output
  • One that changes a global variable
  • One that uses loops

Answer: One whose output depends only on its inputs and has no side effects. It always gives the same result for the same inputs.

What does filter do?

  • Keeps only the items that pass a test
  • Adds all items together
  • Applies a function to every item
  • Sorts a list

Answer: Keeps only the items that pass a test. The test is itself a function.

What does fold(add, 0, [1, 2, 3]) return?

  • 3
  • 6
  • 0
  • [1, 2, 3]

Answer: 6. Fold combines the items into one value.

What is a higher-order function?

  • One that takes or returns another function
  • One with many lines
  • One that is private
  • One that uses arrays only

Answer: One that takes or returns another function. Map, filter and fold are examples.