A pure function is one that always evaluates to the same thing given the same arguments, and cannot change or depend on any external state.
A pure function has special meaning in programming. It is a function that:
- Always evaluates to the same result given the same values
- Does not depend on any external state of the function
- Does not have any side effects
- It doesn't change a mutable object or output to I/O devices
Examples of pure functions include math functions such as sin(x)
which returns the sin of x.
Impure functions may return different results each time they are called. Classic examples of this include time()
and random()
. Additionally, functions that do IO are impure such as printf()
.
Further reading: Pure function at wikipedia