Next lesson playing in 5 seconds

  • Overview
  • Transcript

2.3 Pure and Impure Functions

There are two types of functions in functional programming: pure and impure. You'll learn the difference between the two in this lesson.

2.3 Pure and Impure Functions

There are two types of functions in Functional Programming. The first is a pure function. And almost by definition, the second is an impure function. So we're going to look at examples for both of these in this lesson. So let's create a new file. Let's call this pure.js. And let's first of all talk about a pure function. This is what we want to strive to write because they are easy to test. They are also easy to predict. Because they always return the same result with the same input. So here are some rules for a pure function. It can only work on the data that is passed to it. It cannot work on any external data. It has to solely depend upon its input, which in this particular case are two parameters. Now, it doesn't necessarily have to do anything with those two parameters. But it has to return the same result for the same input. So if we call add, we pass in 1 and 2, the result is 3. If we call add again, pass in 1 and 2, the result is of course 3. If we call add passing 1 and 2, you get the idea. No matter what we pass to add, it is going to give us the sum of those numbers. And if we pass those same numbers, we are going to get the same result. This is a pure function. Now let's look at another function. We'll call this add2. It's going to accept x and y. And it's going to return the result of adding those two things together. But it's also going to write the result to the console. Now in this particular case, this is not a pure function, this is an impure function. First of all, because it relies upon external data or external information, the console is external. It was not passed to the function itself. It also has a side effect, it writes something to the console. Now that might not sound like a deal breaker, but it is when it comes in terms of a pure, impure function. There is a side effect to calling add2. It might give us the same results if we pass in the same information, but it is mutating the state of our application by writing something to the console. So let's add a comment here, this is external and it produces an observable side effect. So then what would be considered an observable side effect? Well, quite a bit and just about anything useful that we would need to do within an application. Such as HTTP requests, reading or writing to local storage, querying or manipulating the DOM or even just simply changing state. All of that is considered an observable side effect. And it makes our functions impure. And if you are like me, you take the term impure with a negative connotation. I mean, who wants to be impure? But it's not really about that. Yes, we want to write pure functions. Because pure functions are consistent. And therefore, they are easy to test and predict. So when it comes to writing our programs, yes, we want to write pure functions, and we want to write as many pure functions as possible. But in order to do anything meaningful, we will have to write impure functions.

Back to the top