Hello guys and welcome back.
So we're going to learn about function pointers now so function point is I like normal pointers.
Apart from we point to functions instead of variables.
Right.
So let's first create a function called My func that takes no arguments.
So put a void in the arguments that I want to print f hello world.
OK.
Now as you know you can call that like this and it will work just fine.
But we don't want to call it like that.
We want to call it through a pointer because the the the function point is allow you to point to functions
and then call that function through the pointer.
Pretty handy feature right.
So to do this go void brackets asterisk Piti off a pointer and then do two brackets again and then we're
going to point that my func the address of my function.
So let's explain this a little bit then.
So we create a pointer to a function and this pointer expects us to point to functions that return type
Lloyd.
So functions that return void.
It also expects us to point to functions that take no arguments in our case we just avoid for no arguments.
Right.
And this should be pretty self-explanatory now.
PTA is the point a name.
So that is how you do function.
Point is.
So if we do something like PTA now like that PTA and we do that we basically call our function through
the pointer.
So when we do PTI it says OK what is the PTA function pointer pointing to.
Oh it's pointing to the address of my funk.
So then let's run the function at the address of my funk and then it literally run the my funk function
because Ampersand my funk gets the address of the function where it is in memory and then PTA contains
the address the memory address of the function and then when we do PCR and we go to call it like this
it goes and finds the address in the PTI variable because we're in the point variables are contain addresses
and then it goes and says OK we have the address let's run that and it'll run at the address that is
stored in this PTI variable which in our case is my fund.
So let's just compile at a minute and run that one second and you'll see hello world.
So that is basically function pointers for you.
So just to demonstrate let's make another function called Test func.
It also takes avoid print k test function.
Okay.
Nice.
So now if we change this here to test fund and then we can pull out again.
It says undeclared.
Why.
Oh because I accidentally did a capital.
So Casey is case sensitive guys.
So we run that now and print case we print f and we run that now and then we do Main and it says test
function.
So essentially what's happened is a pointer now points to test function.
And then when we call points I like this.
It runs test function so you can see how this is put together right.
So the function pointer takes a return type.
It has a variable name and it has function arguments.
That's how it works.
So if we was to create another function let's call it my sum.
You know we did the mice some before.
Let's do that again
and we go return.
A plus B and less create a new pointer for that.
So how does it work.
Well you do um integer brackets asterisk PTI PTI is the function name by the way and you go in it and
it equals and my sum.
So that is how you make a function pointed to this function.
So you should be able to see the resemblance what's going on here.
Right.
In this case our function pointer expects a return type of integer and it expects to arguments that
are both integer.
So you notice we don't provide variable names here because we don't have to write.
Now if I do PTA 50 50 50 25 just show you that different.
And I go integer result equals PTO fifty twenty five.
I can then do print if I result and if we can pull that now
you see seventy five.
So can you guys understand how that is working.
So once more than just in case we create a function pointer that expects a return type of integer it
has a variable name of PTA.
So this could be anything this could be ABC.
Right.
And then you'd have to call it like that.
Right.
And then it takes two arguments integer and integer so this is basically saying we want our function
pointer to look like this.
So any function we point to has to look like this.
It has to return integer.
And it has to take two integers and obviously the ampersand here gets the address in memory of the my
some function.
So this will return some sort of number.
And this number is the memory address.
And then when we call ABC it will go in look at our memory address and it will start executing at that
I mean at that memory address essentially running the my sum function.
So that is how function pointers work.
Hope that sums add up.