Skip to content
IT glossary
ProgrammingUpdated December 3, 2026

Pure function

Чистая функция

A function is pure if the same inputs always produce the same result and calling it has no observable side effects, such as changing an input object or writing to a log.

Two checks for purity

First, ask what determines the result. A pure function takes its varying inputs from its arguments, not from the clock, a random number generator, or mutable settings. Then ask what happens besides returning a result. If the call changes an object owned by the caller, writes to a log, or sends a network request, it has a side effect. Both checks matter: logging a sum doesn't change the sum, but it does make the function impure.

Purity makes computations easier to reason about. Think of a function double that only multiplies its argument by two. You can replace double(4) with 8 without changing what the program does. If the function also incremented a shared counter, that substitution would skip the increment. This substitution property is called referential transparency.

Example: making time an input

function isExpired(expiresAtMs, asOfMs) {
  return asOfMs >= expiresAtMs;
}

isExpired(5000, 4999); // false
isExpired(5000, 5000); // true

If isExpired called Date.now() internally, the same expiration timestamp could produce different answers on successive calls. Instead, the caller can pass Date.now() in production and a fixed timestamp in a test. The comparison stays pure, and the test can check the exact expiration boundary without waiting or mocking the clock.

Where it pays off

Pure functions are useful for pricing rules, input validation, and state transitions. A Redux reducer, for instance, calculates the next state from the previous state and an action. It doesn't fetch data or mutate the previous state. Keeping those responsibilities separate lets you test the transition with plain inputs and expected outputs, without booting the rest of the application.

Common interview pitfalls

Interviewers often show a function that looks like a simple data transformation and ask whether it is pure. Watch for methods such as JavaScript's Array.prototype.sort(), which updates the original array. Sorting a fresh copy of an array of numbers is fine: purity doesn't forbid loops or local mutation. But a shallow copy still shares any nested objects with the original, so editing their properties can affect the caller. Also check any helpers the function calls: an outer function can appear to do nothing but return a value while a helper writes to a log.