‫What is a pure function?
‫Well, a pure function looks like this.
‫We have a function.
‫So instead of a box that we have before of objects of all these functionalities, instead think of this
‫as just a simple function.
‫And let's say we give it a list containing one, two and three.
‫If we give this input into this function, what we would expect with a pure function is for something
‫to happen to this input that every time results in the same thing.
‫So let's say we multiply this by two, we would receive two, four and six.
‫Now a pure function has two rules.
‫One is that given the same input, it will always return the same output.
‫That is, every time we give one, two and three to this list, we give it to a function that we create.
‫It should always return the same output.
‫So if I run this function millions of times with the same input, it should result in the same output.
‫And the second point is this idea of a function should not produce any side effects.
‫What are side effects?
‫Side effects are things that a function does that affects the outside world.
‫For example, if I was to print something inside of this function, it affects the outside world, right?
‫Because I'm printing something onto a screen.
‫The screen is the outside world.
‫Or for example, if this function was touching a variable that lived outside of a different scope,
‫that's a side effect.
‫Let's have a look at the code to understand this concept a little bit more.
‫Let's say we had a function and then this function will multiply.
‫By to a list.
‫So let's just say an ally, which will be our list.
‫And in here, what we want to do is loop over the item.
‫In the list.
‫And then each item we want to multiply and append to a new list.
‫So let's create a new list that will be an empty list.
‫And in here we'll say new list, dot append.
‫And do item multiply by two.
‫And then finally, at the end of this function, we want to return the new list.
‫Let's give this a go.
‫If I say multiply by two and give it one, two, three, and let's print out.
‫Here the output.
‫If I click Run.
‫I get two for six?
‫Is this a pure function?
‫Let's think about this.
‫The first rule, given the same input, that is one, two and three.
‫It will always return the same output.
‫No matter how many times I run this, it's always going to give me the same output.
‫So we passed the first test.
‫What about the second test?
‫That it produces no side effects.
‫Does this produce any side effects?
‫Does this touch anything in the outside world?
‫Think about it.
‫No, it doesn't.
‫Right.
‫Nothing in the outside world matters to this function.
‫Because everything is contained in here.
‫If, for example, instead of having print here, we simply have print.
‫And here.
‫If I click Run.
‫We get the same output.
‫But does this have side effects?
‫Well, yes, it does.
‫It interacts with the outside world, which is the print statement.
‫The print statement tells me, hey, I'm going to print on whatever the display this machine has.
‫In our case, it's going to print here, but we don't really know what print statement does because
‫it interacts with the screen on this side.
‫So this.
‫Has side effects.
‫We give control to print, but we don't know what print is going to do.
‫Another example that I can give you.
‫Is this what if we have a new list here that we actually define outside?
‫Like so.
‫So that we have new list like this.
‫If I click Run.
‫I still get two for six.
‫But does this have side effects?
‫Well, yes, it does.
‫It interacts with the outside world.
‫And what's outside of this world of a function which, remember, is the scope.
‫Well, it interacts with this new list.
‫It opens to this new list.
‫So if a programmer comes along and maybe before we run this function, we decide to say new list.
‫We change it to an empty string.
‫Well when I run this function.
‫I get an error because now this new list is a string object.
‫So the new list that lives in the outside world of this function can be modified by another developer
‫or by a program, and we would have know about it.
‫And when we run this function, it's going to give us an error.
‫It has a side effect.
‫So ideally we contain our functions.
‫And make them pure because as you can see, we're never going to have a bug or an error in our code
‫unless we wrote something wrong here.
‫But because we don't care about the outside world, because everything that we give as an input is always
‫going to produce a good output, it's going to be pure.
‫Now, it's hard to really understand why this is such a powerful concept without having a bit of experience
‫in the workplace.
‫But let me just tell you this, that when you have pure functions.
‫You have less buggy code, you're able to test your code better.
‫It's easier to understand your code and overall.
‫You have these benefits of not having different parts of your code touching each other and affecting
‫each other, which makes your life as a programmer so much easier.
‫So this idea of pure functions is really important.
‫But the one thing I want you to take away from this is that pure functions is more of a guideline than
‫an absolute.
‫It is impossible to have pure functions everywhere, because if a function doesn't affect the outside
‫world at all, well, we wouldn't have any programs.
‫We wouldn't be able to display things.
‫We wouldn't be able to save things such as our game profile.
‫So it's impossible to have pure functions everywhere.
‫However, when you can, wherever you can, functional programming teaches us that your functions are
‫good.
‫Try to create pure functions and only have few non pure functions that maybe interact with the outside
‫world that we can go back to whenever we have bugs in our code.
‫Because most likely those are the ones that are going to have bugs then pure functions like this.
‫To finalize our concept of functional programming.
‫If, for example, this was our wizard from the previous videos with functional programming, we would
‫have a wizard object.
‫Or Wizard dictionary that had name.
‫Let's say Merlin.
‫And power.
‫Of 50.
‫And instead of containing these in a class, I would have different methods such as attack.
‫And this wouldn't be a method.
‫It would be a function because it doesn't live inside of a class.
‫In functional programming.
‫They say, Hey, we don't need to combine data with functions, let's keep those separated.
‫Let's just create pure functions that can be passed.
‫Anything like a character.
‫And this way we keep those two things separate so that we can focus on pure functions and we can focus
‫on data.
‫So that's where the difference is.
‫At the end of the day, it's all about trying to keep our code clean and avoiding bugs in our code.
‫A big problem that programmers have all the time.
‫So it's just a different way of thinking.
‫Let's learn some more in the next video.