Published on

# What the hell is a Pure Function?

Authors

I was reading the React docs and came across this

React is pretty flexible but it has a single strict rule. All React components must act like pure functions with respect to their props.

Unfortunately, I didn’t know exactly what a pure function was, and since I’m a react developer I thought it would be a good idea to know what the hell is a pure function. This is why I decided to write this post.

A pure function is simply described as:

1. Its return value is the same as the value being passed as props.
2. Its evaluation has no side effects such as changing valuable outside the function.

A pure function is like a math problem. 2 + 2 is always 4. That problem does not have any side effects. It will always return 4. You will never change the value of 2 and get a different answer.

Here is an example, of a function that is not a pure function. It has a side effect and that side effect is it changed the global array variable.

const array = [1, 2, 3, 4]

a.push(element)
}



Array now equals [1, 2, 3, 4, 5]. If it was a pure function the array variable would never change but the return value from the function would by [1, 2, 3, 4, 5].

How you can my make the above function a pure function is like this:

const array = [1, 2, 3, 4]