hey what's going on folks it's mike here and welcome to the next lesson in the c plus plus series now we've been talking previously in the series about pointers quite a bit and that pointer store and address now recall that an address could be of a variable or it could be as a function because well functions have addresses too so in this lesson we're going to talk about function pointers and see how they work and why we might want to use them so with that said let's go ahead and dive in so what i've got here is a little example set up here where i have two functions here add and multiply both take in two arguments an x and a y and perform the resulting operation so if we wanted to perform one of these operations we could call add and do 2 comma 2 for example or we could also do our multiply here so let's go ahead and add multiply here and i'll compile this program and i'll run it and in fact this probably isn't a good example to do two and two so just a testing tip try to use different or weird numbers to confirm that each of our operations are getting the right results here so i have four and six here all right now where does a function pointer come into well instead of explicitly calling this function we can instead have a pointer to the add and the multiply functions and even change what that pointer points to over time so maybe let me show you the example and how this works and then it'll make a little bit more sense so let's say at run time we want to be able to choose one of these functions add or multiply so i can create a function pointer so how do i do that well the data type is again part of a function here here's the return type and some operation that we want to do here and then what we do is the arguments so i have something here that sort of looks like the signature of this function multiply here int for each of the arguments here the first and the second these are the parameters and then i have the name of my actual function pointer which i'm just going to call up short for operation and then the star here which again we've been seeing before with the data type when we declare it now here is actually the return type so the function is a little bit weird but this is a function pointer for functions that have a signature of int function name and two parameters which is exactly what we have in this program that's why we have declared this in fact at this time i usually like to just save and compile to make sure that i have the syntax right and that's what it looks like now i'll show you an easier way to create function pointers in modern c plus plus if you hang around to the end of the video but let's just go ahead and see how this works for now okay so now that i have my function pointer well we're going to do the same thing that we do with our pointers we have to point them to something and we do that with the equals operator so what i'm going to go ahead and do is say up equals add and really that's it so now we're pointing to the add function and the signature matches we have two ends here as a parameters and an ins as a return type so now instead of calling add i'm just going to call this operation and let's go ahead and rerun our program and we'll see that the appropriate function is called so our function pointer here points to the add function and knows to pass in the two values 2 and 2 into x and y and return in integer now why this is cool or useful well we can change what this operation points to at different times so often you're going to see this in programming when you want to use a function pointer as sort of a callback function for example if you've ever done or used rather any graphical user interface and let me go ahead and just draw an example here where you have some button and the user comes over and clicks on the button usually some function is called and that function is usually a callback function which this thing is the function pointer ptr short for pointer and that will call some function that says you know what to do whenever the button is pressed so that's how function pointers are often used and in fact we can sort of have our own version of this where well let's go ahead and get some user input i'll go ahead and make this a little bit smaller so you can see the full example on the screen here and let's go ahead and read in a value so we'll write a little program here that says 1 for add or two for multiply and we'll read in the user input and we need a integer n which we'll read in this input to and we'll just say if n equals 1 then our operation is an add otherwise else if n equals 2 up equals multiply so we can see how we're dynamically going to change the behavior of our program here so let's go ahead and just simplify this a little bit and just to make this a little bit more of a calculator we should probably also read in two values for the values that we want to multiply or add together so i'm simply going to read those in as x and another value for y and we need integers for those and then we can do our appropriate operation on x or y so let me make this a little bit smaller so you can see all the code here and we'll recompile and oops i just need to make sure that i make this a sea out statement so i'll fix that up compile and rerun so what do we want to do let's go ahead and add first and two numbers three and seven and we get ten and i'll rerun this again this time let's see a multiply how about seven and uh nine which should be 63 so we can see the function has been appropriately pointed to multiply and then the multiply operation all right so that's it for function pointers or at least one use case where you can see them again you'll see these in the code they'll look a little bit scary sometimes but this is often how they'll be declared now we haven't talked necessarily about typedef but i want to give you an easier way that you'll also see this in the code so let me go ahead and just show an example where i can provide a more humid readable type definition and i'll say let's just call this integer operations and then when declaring this instead of this sort of weird syntax i can instead do something like this where i say we have some integer operation and usually we'll see these prefix with something like p f n for function pointer so i'll go ahead and add that here p f n and let's make that integer operation for integer operations and then we can likewise just create our up just like we did previously okay so let me just go ahead and compile this just so you can see it works just the same here so add and then two numbers four and five and we get 9 here okay so this is just a little trick that you'll often see in header files if there's lots of use of function pointers especially in c code bases or code bases where there's c and c plus plus mixed together now i did promise you that there's also yet another easier way to work with function pointers and that is with standard function so let me go ahead and bring up our favorite web page here cpp reference i'm going to just move it over to the side here and we're going to look up standard function and see that we have a way to create function pointers with something called standard function and this is a way to wrap something that is callable now there's a little example here showing how it's used but i want to show you how we would do the same thing that we did previously here so what i'm going to do is first include the appropriate header or if you need to see again this is how i figured out which header to include so functional and the syntax for this is well it's part of the standard library and then we put in the function signature so what it's going to return and the arguments those are what goes between the brackets and we have to talk about templates yet in the series but you'll see here and again we could create our up here just like we previously did so i'll recompile this re-run it let's try multiply i'll do six and four so get a value of 24. so we can see this is working just the same so whether you want to use the type definition to make things a little bit easier to type out use the actual function pointer syntax or use the modern style standard function you now understand that we're able to create pointers to functions so folks that's a bit of a wild lesson or maybe it's something that's new but now you know how you can change the behavior or what functions are called in your program this is again a really powerful thing it's used very frequently in event driven or graphical user interface programming where you need to change the behavior based off of what button was clicked or something that the user did so i hope you enjoyed something in this lesson and learned a lot from it and if you did enjoy or learn something then go ahead and hit the like and subscribe button and we'll hope to catch you in the next video thanks for your time folks and i can't wait to see in the next one
Get free YouTube transcripts with timestamps, translation, and download options.
Transcript content is sourced from YouTube's auto-generated captions or AI transcription. All video content belongs to the original creators. Terms of Service · DMCA Contact