# Pure Function

> A function that gives the same answer for the same input. It changes nothing else.

**Field:** Software Development  
**Category:** Programming Paradigms  
**Also known as:** Pure Functions

## Think of it like

Think of it like a calculator. Press 2 plus 3, and you get 5 every single time.

## Definition

A pure function always gives the same output for the same input. It does nothing else: it does not change a variable outside, write a file or print. Everything it needs comes in through its inputs. That makes it easy to test, since you only check what comes back. It is the main building block of functional programming.

## Example

Jack's add(2, 3) gives 5 today, tomorrow and on every computer. His test only checks that one number.

## Example code

```python
def add(a, b):
    return a + b

print(add(2, 3))  # 5
```

Same inputs, same answer, and nothing else is touched.

## Why it matters

Pure functions can be tested alone, cached and run in any order without surprises.

## Common mistake

Reading the clock or a random number inside the function. The answer then changes even when the input does not.

## Three principles

- **Same in, same out.** Every time, everywhere.
- **No side effects.** Nothing outside is changed.
- **Easy to test.** Check the answer, nothing else.

## Related terms

- [Side Effect](https://learn.benibix.com/term/development/side-effect)
- [Functional Programming](https://learn.benibix.com/term/development/functional-programming)
- [Function](https://learn.benibix.com/term/development/function)
- [Immutability](https://learn.benibix.com/term/development/immutability)

---

Source: https://learn.benibix.com/term/development/pure-function
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-17
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
