We've seen the usage of pointers with variables.
Up until this point and just like variables functions also have a specific address in memory.
We can exploit this fact and actually have pointers that point to functions and they're called function
pointers.
So here's how to actually make a function pointer.
So we have the function and say return type here and in these parentheses we have the star and whatever
the function pointer you want things function pointer and these parentheses will actually be the argument
just OK.
So you can actually make a function pointer with a specific signature.
So this so the signature will really depend on what the argument list is and what the return type is.
And we can actually have mentors that point to it.
So let's see an example of this.
That's And let's say we had a function that took the sum of some array.
This in the like A and let's say we had a similar function that said where are we in terms of the signature
and that the average the rate
the length ears wow
and let's say we also had one other function and say it's the output and it's going to take the array
and the length
and it's actually going to take a function pointer
like it's
the array
in one way.
So this output function actually has three arguments.
Has the array has the length of the array and it has this thing which is a pointer to a function K and
that pointer to a function must have as its return type an integer and must have as its arguments an
array and the length k.
So you can kind of see where I'm going with this.
Both of our functions have as the arguments the length and the array and both of them also return an
integer.
So let's implement these implement these first two or simplest
and some will be simple some Sphero sleep over this this like a sports and some sequels array of
which you turn the song and for average all we have to do is get some of the re link here.
And we just divide it by the way Frank.
So those are those two functions.
And now this output function.
So copy this
piece here and this will do is output the results of whatever we pass into it.
So the results is and how you use the function pointer is you just say the name of the function pointer
is a function.
In this case and we pass it the these two arguments.
So these these these arguments just define what the function pointer has as its are you in.
It's not the arguments itself.
So these are the arguments themselves which passed like this.
And all their K.
And now how to use this output function.
So to re say it's the even numbers 2 4 6 8 10 10.
So now if we want to use help we just output array and the like.
5.
In this case and let's say the sup.
So notice I'm using just the name of the function k.
So this is kind of like the address of it and use the name instead.
And notice I'm not using any kind of argument list.
This just is really just the name.
And here I'll also do it for the average.
This right.
So this is doing is it's actually passing the address of the function to this output function here.
So passing it to here and it's actually calling the correct function.
So the first case would be some so would be passing some as the function and I would actually call some
here.
And so this is actually call some and it would do everything that some did and output the sum and do
the same thing with the average So let's do this and see what happens.
So the result is 30.
So in this case we're just adding two four six eight ten which equals 30 and we're actually outputting
the sum using this function pointer and the next one 6 so the average of all this is 6.
Makes sense.
The middle one right.
And it's outputting by calling average down here and using the array and the length actually calls this
function and does all this.
So function pointers are they can be quite useful in some contexts.
We will be really using them in in the next game we have which is battleship but we will use a similar
concept to function pointers and Space Invaders later on.
So I just want to show you this kind of interesting way you can use pointers to functions.
They really do come in handy in some contexts.
And in fact we'll see one of those in the practice in Dr..