WEBVTT

00:00.110 --> 00:00.620
Okay guys.

00:00.620 --> 00:02.240
So pure functions.

00:02.240 --> 00:04.760
That's an example of a pure function.

00:06.590 --> 00:12.050
When you run a pure function with the same arguments, you always get the same result.

00:13.040 --> 00:16.700
So the first round, second round it is exactly the same.

00:19.040 --> 00:22.190
Now an example of a non pure function.

00:23.180 --> 00:29.240
So this one has some dependency on a shared state and has side effect.

00:30.320 --> 00:33.260
So it modifies the global state.

00:33.770 --> 00:37.850
When this run for the first time the result is five.

00:37.880 --> 00:41.600
When it's run for the second time the result is ten.

00:41.630 --> 00:48.650
Even though you supply the same arguments, that's because it has side effects and it is dependent on

00:48.650 --> 00:50.030
the shared state.

00:52.400 --> 00:54.560
So let's actually write this code.

00:54.560 --> 00:59.810
Let's create a pure PHP file, open it right away.

00:59.810 --> 01:05.720
Are the PHP tag and implement this add function, which is pure.

01:05.990 --> 01:08.090
It has two arguments.

01:08.090 --> 01:10.070
We can also type them as.

01:10.100 --> 01:11.090
Why not?

01:11.630 --> 01:13.250
So those are integers.

01:13.280 --> 01:19.070
The return type is also an integer and we return a plus b.

01:22.760 --> 01:23.390
Like that.

01:23.390 --> 01:31.880
Now if I echo or vardump the result of calling this function, I should get the same result every single

01:31.880 --> 01:32.660
time.

01:33.170 --> 01:37.400
So I'm calling add with those two parameters.

01:38.030 --> 01:42.590
Maybe let's do that again and just var dump this immediately.

01:42.830 --> 01:48.050
I need to jump to functions folder and run pure PHP.

01:48.830 --> 01:53.210
Now as you see we've got two exactly the same results.

01:55.130 --> 01:57.620
Now let's do the second example from the slide.

01:58.280 --> 02:04.230
So define the function called add to total that just accepts a value.

02:05.850 --> 02:06.630
But this one.

02:06.630 --> 02:11.370
This is using a global variable that I'm going to define right here.

02:13.530 --> 02:18.180
And to use this variable I need to use the global keyword.

02:18.210 --> 02:23.940
And now I have access to the original total variable from outside this function.

02:25.650 --> 02:30.300
So if I add something to total.

02:32.640 --> 02:41.490
And I return it then as you've seen on the slide, this actually is influencing some outside state,

02:41.490 --> 02:45.120
some variables that are outside this function.

02:45.120 --> 02:47.370
And this is called a side effect.

02:47.970 --> 02:55.410
So if you are calling a function and you would like to actually calculate something, but instead a

02:55.440 --> 03:00.970
global state is modified which you might be aware of or you might not be.

03:01.000 --> 03:04.570
Then this function is not considered pure.

03:08.950 --> 03:10.300
Now let's run that.

03:12.040 --> 03:17.320
So I'm going to var dump two calls to this function with the same parameters.

03:17.350 --> 03:19.390
So this is maybe.

03:19.750 --> 03:21.550
Sorry that's just one parameter.

03:21.550 --> 03:26.590
Let's maybe call with three and another call with the same one.

03:26.590 --> 03:29.590
So we can see the different output.

03:29.980 --> 03:32.020
So we are calling pure PHP.

03:32.260 --> 03:34.060
The first call returns three.

03:34.090 --> 03:36.430
The second call returns six.

03:38.170 --> 03:40.030
Okay so what's the point.

03:40.030 --> 03:42.850
Well the pure functions they are predictable.

03:42.850 --> 03:47.110
You always know that you're going to get the same output for the same input.

03:47.680 --> 03:52.900
It's easier to test such functions and they are easily reusable.

03:53.680 --> 03:57.190
Also the result of such functions can be cached.
