WEBVTT

00:00.610 --> 00:03.430
We've seen the usage of pointers with variables.

00:03.430 --> 00:12.010
Up until this point and just like variables functions also have a specific address in memory.

00:12.310 --> 00:20.620
We can exploit this fact and actually have pointers that point to functions and they're called function

00:20.620 --> 00:22.120
pointers.

00:22.120 --> 00:27.330
So here's how to actually make a function pointer.

00:27.410 --> 00:42.700
So we have the function and say return type here and in these parentheses we have the star and whatever

00:42.700 --> 00:53.150
the function pointer you want things function pointer and these parentheses will actually be the argument

00:53.380 --> 00:56.270
just OK.

00:56.640 --> 01:03.740
So you can actually make a function pointer with a specific signature.

01:03.740 --> 01:11.090
So this so the signature will really depend on what the argument list is and what the return type is.

01:11.090 --> 01:13.960
And we can actually have mentors that point to it.

01:13.970 --> 01:17.400
So let's see an example of this.

01:17.600 --> 01:26.790
That's And let's say we had a function that took the sum of some array.

01:27.090 --> 01:39.070
This in the like A and let's say we had a similar function that said where are we in terms of the signature

01:39.790 --> 01:43.960
and that the average the rate

01:46.760 --> 01:48.440
the length ears wow

01:51.550 --> 02:02.020
and let's say we also had one other function and say it's the output and it's going to take the array

02:02.890 --> 02:03.670
and the length

02:07.210 --> 02:12.030
and it's actually going to take a function pointer

02:15.850 --> 02:17.200
like it's

02:20.150 --> 02:21.040
the array

02:24.230 --> 02:27.020
in one way.

02:34.240 --> 02:38.380
So this output function actually has three arguments.

02:38.440 --> 02:48.190
Has the array has the length of the array and it has this thing which is a pointer to a function K and

02:48.190 --> 02:58.060
that pointer to a function must have as its return type an integer and must have as its arguments an

02:58.060 --> 03:01.850
array and the length k.

03:02.160 --> 03:05.190
So you can kind of see where I'm going with this.

03:05.190 --> 03:12.900
Both of our functions have as the arguments the length and the array and both of them also return an

03:12.900 --> 03:14.220
integer.

03:14.280 --> 03:20.070
So let's implement these implement these first two or simplest

03:22.920 --> 03:44.020
and some will be simple some Sphero sleep over this this like a sports and some sequels array of

03:47.180 --> 04:00.750
which you turn the song and for average all we have to do is get some of the re link here.

04:01.320 --> 04:06.410
And we just divide it by the way Frank.

04:06.480 --> 04:09.490
So those are those two functions.

04:09.750 --> 04:12.130
And now this output function.

04:12.140 --> 04:15.390
So copy this

04:18.580 --> 04:25.470
piece here and this will do is output the results of whatever we pass into it.

04:25.470 --> 04:36.670
So the results is and how you use the function pointer is you just say the name of the function pointer

04:36.910 --> 04:37.750
is a function.

04:37.750 --> 04:46.010
In this case and we pass it the these two arguments.

04:46.270 --> 04:52.410
So these these these arguments just define what the function pointer has as its are you in.

04:52.420 --> 04:54.890
It's not the arguments itself.

04:55.300 --> 05:01.190
So these are the arguments themselves which passed like this.

05:03.180 --> 05:06.370
And all their K.

05:06.800 --> 05:10.630
And now how to use this output function.

05:10.650 --> 05:25.150
So to re say it's the even numbers 2 4 6 8 10 10.

05:25.150 --> 05:33.840
So now if we want to use help we just output array and the like.

05:33.880 --> 05:34.770
5.

05:34.790 --> 05:38.460
In this case and let's say the sup.

05:38.480 --> 05:43.110
So notice I'm using just the name of the function k.

05:43.190 --> 05:48.190
So this is kind of like the address of it and use the name instead.

05:48.380 --> 05:50.860
And notice I'm not using any kind of argument list.

05:50.860 --> 05:52.910
This just is really just the name.

05:53.480 --> 06:00.380
And here I'll also do it for the average.

06:01.130 --> 06:04.090
This right.

06:04.800 --> 06:15.290
So this is doing is it's actually passing the address of the function to this output function here.

06:15.380 --> 06:22.960
So passing it to here and it's actually calling the correct function.

06:23.310 --> 06:30.060
So the first case would be some so would be passing some as the function and I would actually call some

06:30.060 --> 06:31.920
here.

06:32.010 --> 06:40.700
And so this is actually call some and it would do everything that some did and output the sum and do

06:40.730 --> 06:46.130
the same thing with the average So let's do this and see what happens.

06:47.080 --> 06:49.810
So the result is 30.

06:49.810 --> 06:59.320
So in this case we're just adding two four six eight ten which equals 30 and we're actually outputting

06:59.440 --> 07:08.130
the sum using this function pointer and the next one 6 so the average of all this is 6.

07:08.140 --> 07:08.910
Makes sense.

07:09.160 --> 07:10.270
The middle one right.

07:11.680 --> 07:21.490
And it's outputting by calling average down here and using the array and the length actually calls this

07:21.490 --> 07:25.720
function and does all this.

07:26.100 --> 07:32.310
So function pointers are they can be quite useful in some contexts.

07:32.350 --> 07:41.130
We will be really using them in in the next game we have which is battleship but we will use a similar

07:41.130 --> 07:45.460
concept to function pointers and Space Invaders later on.

07:45.480 --> 07:53.800
So I just want to show you this kind of interesting way you can use pointers to functions.

07:54.540 --> 07:56.830
They really do come in handy in some contexts.

07:56.850 --> 08:01.660
And in fact we'll see one of those in the practice in Dr..
