Function - Pure Function

> Code - (Programming|Computer) Language > (Function | Operator | Map | Mapping | Transformation | Method | Rule | Task | Subroutine)

1 - About

Pure functions are called pure because they do not attempt to change their inputs, and always return the same result for the same inputs.

The result is then predictable.

Pure function take data and transform it, without modifying any shared state, meaning there is no side effects besides returning a value.

They are mostly used in functional programming.

Advertising

3 - Example

Pure

function sum(a, b) {
  return a + b;
}

Not Pure

function withdraw(account, amount) {
  account.total -= amount;
}

4 - Data Structure

Pure functional data structures - Purely Functional Data Structures - Chris Okasaki - September 1996 - CMU-CS-96-177 (PDF)

5 - Documentation / Reference