What's a Pure Function?
Tags: javascript, functional
Published: 2020-07-21 02:08:43

This week I was exploring more interview prep materials and stumbled upon the topic of Pure Functions in JavaScript. I thought it would be a good concept to review because likely other people are just as confused by them at first as I was. My first thoughts when I learned of the concept of pure functions were something along the lines of "Wait, does that mean there are impure functions?" and "Does that mean one is preferable over the other?". 

The immediate answer to those questions is "Yes, a function that is not pure is by definition impure." and "Kind of, depending on the situation.", but let's take a step back. Before we dive into talking about types of functions, we should probably just talk about functions in general first. Let's pull out our handy dandy documentation and look at some definitions.

MDN documentation defines functions as so:

>Functions are one of the fundamental building blocks in JavaScript. A function is a JavaScript procedure—a set of statements that performs a task or calculates a value. To use a function, you must define it somewhere in the scope from which you wish to call it.

Okay, so a function is a bit of code that takes an input, does some things, and outputs a value. How does that help us? Well, the key to determining whether a function is pure depends completely on what is happening during that "do something" part. Now that we have that little bit out of the way, let's talk about what exactly a pure function is.

There are only two requirements for function to he considered pure:

1. Same input - same output. This means that as long as the same input is passed into a function, the same output will always be returned.
2. No side effects. This means that the function does not change anything outside of itself. No network requests, no mutating outside data, no writing to disk, etc.

Take a look at the two functions below and see if you can figure out which one is pure and which one is not. 

```javascript
// Function 1

function multiply(num, mult){
   return num * mult;
}

// Function 2

var mult = 2;

function multiply(num){
   return num * mult;
}

```

If you guessed that the first function is the pure one, you'd be correct. `multiply(2,2)` will return `4` every single time it is run and does not perform any outside mutations. This is the essence of a pure function. 

However, the second function relies on outside data for its computation. Because we have to rely on the global `mult` variable, `multiply(2)` could return `4` on one call, `6` on the next, and so on. I think you can begin to see some situations in which pure functions may be considered "better" than impure ones.

Pure functions are not always the best solution to a problem. Sometimes we want to change outside data, make a network request, or perform some other operation outside of a pure mapping computation. That's one of the reasons the answer to "Are pure functions better" is both yes and no. It depends on the context.

The thing about pure functions, and what often times makes them the better solution, is that they are simple in their use. That is, you needn't worry that a pure function will cause problems elsewhere because it doesn't touch anything outside of itself. This creates less room for bugs and side effects, and also makes refactoring easier as you only have to be concerned with the code inside the function. 

In summary, pure functions are a great way to tidy up your code and they are a great way to keep your code scalable and bug free. However, they aren't always the best tool for the job. A good balance and the proper utilization of pure and impure functions can make for better and more durable code.

Check out these resources if you'd like to learn more:

[Functions - MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions)
[JavaScript: What Are Pure Functions And Why Use Them?](https://medium.com/@jamesjefferyuk/javascript-what-are-pure-functions-4d4d5392d49c)
[What Is a Pure Function in JavaScript?](https://www.freecodecamp.org/news/what-is-a-pure-function-in-javascript-acb887375dfe/)

Also feel free to follow me elsewhere on [Twitter](https://twitter.com/AJBanderas), [Github](https://github.com/kjdowns), or [LinkedIn](https://www.linkedin.com/in/kevinjdowns/).