Skip to main content

Pure Function

In Turkish
Saf fonksiyon
Updated 2 min read

Share this page

Send the link, quote the definition with a link back, or show it as a card on your own site.

https://softwaredictionary.org/terms/pure-function

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

A pure function next to an impure onejavascript
// 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

Spotted a mistake or something missing on this page?Suggest an edit

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings