Pure Function
- In Turkish
- Saf fonksiyon
In short
A pure function always returns the same output for the same input and has no side effects, meaning it does not change anything outside itself.
What is a pure function?
A pure function follows two rules. First, its result depends only on its arguments: call it with the same inputs a thousand times and you get the same answer every time. Second, it has no side effects, meaning it doesn't modify outside variables, change its arguments, write to files or databases, print to the screen or make network calls.
A function becomes impure as soon as it reads or changes hidden state. Math.random() and a function that reads the current time are impure because their results change between calls, and a function that pushes items into an array passed in as an argument is impure because it changes data the caller owns. Pure functions are referentially transparent: any call can be replaced with its result without changing how the program behaves, which makes it safe to cache results, a technique known as memoization.
A calculator's square-root button is a good analogy: pressing it for 16 always shows 4, and it doesn't change anything else stored in the calculator. Pure functions are easy to unit test, because you only need to check inputs and outputs, and they are safe to run in parallel. They are central to functional programming and appear in everyday code as reducers, formatting helpers and calculation logic.
Pure functions are sometimes confused with idempotent operations. An idempotent operation, such as setting a user's email to a fixed value, gives the same end result whether it runs once or many times, but it still has a side effect because it changes stored data. Real programs need side effects, so the usual approach is to keep core logic pure and push work like database writes to the edges of the program.
Key takeaways
- A pure function returns the same output for the same input, every time.
- It has no side effects: no changes to outside state, arguments, files or the network.
- Pure functions are easy to test, cache and run in parallel.
- Idempotent operations may still have side effects, so they are not automatically pure.
Example
// Pure: depends only on its inputs and changes nothing else
function addTax(price, rate) {
return price * (1 + rate);
}
// Impure: reads and changes state outside the function
let total = 0;
function addToTotal(price) {
total += price; // side effect: changes outside state
console.log(total); // side effect: output
return total;
}Readers ask
Is `console.log` a side effect?
Yes. Writing to the console, a file or the network changes something outside the function, so a function that logs is technically impure, even if its return value is predictable.
Can a pure function call another function?
Yes, as long as the functions it calls are also pure. Calling an impure function, such as one that reads the current time, makes the caller impure too.
Why are pure functions easier to test?
Because their output depends only on their input, a test just calls the function with some arguments and checks the result. No global state, database or mocks need to be set up first.
See also
- FunctionProgramming Fundamentals, p. 21A function is a named, reusable block of code that performs a specific task, optionally taking inputs called parameters and returning a result.
- Functional ProgrammingProgramming Fundamentals, p. 22Functional programming is a style of building software from pure functions that avoid changing shared data, making code more predictable and easier to test.
- ImmutabilityProgramming Fundamentals, p. 27Immutability means a value cannot be changed after it is created, so every update produces a new value instead of modifying the original in place.
- MemoizationProgramming Fundamentals, p. 36Memoization is an optimization technique that stores the results of function calls and returns the saved result when the same inputs occur again.
- IdempotencyBackend & APIs, p. 24Idempotency is the property of an operation that produces the same result whether it runs once or many times, so accidentally repeating a request is safe.
- Unit TestTesting & Quality, p. 35A unit test is a small, automated check that verifies one function, method, or class behaves correctly in isolation from the rest of the program.
Spotted a mistake or something missing on this page?Suggest an edit