Programming languages / Pure functions

Pure functions

What makes a function pure, shown with one small function that is and two that are not, and why programmers go out of their way to keep most of their code that way.

Beginner7 steps~6 min
Input — a value handed to the function
Function — the code that does the work
Output — the value the function returns
Outside — variables, files, screen, network
// THE SHORT VERSION

In 30 seconds

A pure function follows two rules. Its answer depends only on the inputs you hand it, so the same inputs always give the same answer. And it has no side effects: it changes nothing outside itself.

That makes it easy to trust. You can test it with one line, save its answers for reuse, and read it without wondering what else it touches.

Programs still need side effects to read input and save results. The usual approach is to keep those at the edges and the logic in between pure.

Pure functions, step by step

STEPS01 / 07

The examples are JavaScript, which does not enforce purity: it is a discipline the programmer keeps. Printing a message and modifying an argument you were given both count as side effects too.

STEP 01 / 07 Inputs in, answer out
THE MATH
total(price, qty) = price × qty total(4, 3) = 12
  1. Step 1: Inputs in, answer out. A function is a named piece of code that takes inputs and hands back a result. This one multiplies a price by a quantity. It looks at nothing except its two inputs and does nothing except return the answer. A function that keeps to both rules is called pure.
  2. Step 2: Same input, same output. Call a pure function with the same inputs and you get the same output, every time and on every machine. That makes the call and its answer interchangeable: wherever the program says total(4, 3), you could write 12 and nothing would change.
  3. Step 3: Impure: a hidden input. This function subtracts a discount that is not one of its inputs: it reads a variable that lives outside it. When some other code changes that variable, the same call gives a different answer. Reading the clock, a random number, a file or a database breaks the rule in the same way.
  4. Step 4: Impure: a side effect. A side effect is anything a function does besides returning its answer: changing a variable outside itself, writing a file, printing, sending a request. sell returns 12 every time, yet it is not pure, because each call also adds an entry to a list that other code can see.
  5. Step 5: Pure code is easy to test. Testing a pure function takes one line: call it and compare the answer. Testing an impure one means first arranging the outside world it depends on, then putting everything back so the next test is not disturbed. Pure tests can also run in any order, or all at once.
  6. Step 6: Pure results can be cached. Because the answer for given inputs never changes, it can be saved the first time and simply looked up afterwards. This is called caching, or memoisation, and it pays off when the function is slow. Doing the same to sell would be a bug: the cached calls would skip the log entry.
  7. Step 7: Where side effects live. A program with no side effects could not read input, show a result or save anything, so it would be useless. The aim is not to remove effects but to keep them in a thin outer layer that reads and writes, and to leave the decisions and calculations in the middle as pure functions.
VALUES SO FAR
total(4, 3) → 12pure
repeat····rule
price····impure
sell····impure
test····result
cache····saved
effects····rule
// BEFORE

Learn this first

Nothing. This concept starts from scratch.

Spotted a mistake on this page?