What is a pure function.
Well it pure function looks like this.
We have a function.
So instead of a box that we had 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 1 2 and 3 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 affect 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 code to understand this concept a little bit more.
Let's say we had a function and then this function will multiply.
By 2 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 2.
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 2 and give it to 1 2 3 and let's print out
here the output if I click Run I get 2 4 6.
Is this a pure function.
Let's think about this.
The first rule given the same input that is 1 2 and 3 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 okay we pass 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 in 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
so that we have new list like this.
If I click Run I still get to 4 6 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 a pants 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 wouldn't 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 than 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.