What is a Pure Function?

Model Funny

About

Pure functions are called pure because:

  • they do not change their inputs,
  • they always return the same result for the same inputs.

The result is then predictable.

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

They are mostly used in functional programming.

Example

Pure
function sum(a, b) {
  return a + b;
}
Not Pure
function withdraw(account, amount) {
  account.total -= amount;
}

Expression

A pure expression is an expression that declares data and transform it with functions.

Impurities

Impurities are language constructs that are not reproducible.

Data Structure

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

1) 2)





Discover More
Data System Architecture
(Data Type | Data Structure)

A type is the classification of value according to: how they are represented (the structure) the properties that they have (the operations) A data structure is a complex data type that can not be...
React Props Chrome Plugin
React - Props

props is the only object argument passed from a component to another component. Props are similar to State, but is public and not fully controlled by the component. You can also pass arguments to...
Card Puncher Data Processing
What is Functional programming (FP) - Collection Operations

In Functional programming (FP), you will pass functions to other functions as parameters. It is a declarative paradigm that treats computation as the evaluation of mathematical functions. With functional...



Share this page:
Follow us:
Task Runner