Function Pointers in C++

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up guys my name is Jonah welcome back to my state plus plus series so today we're gonna be talking all about function pointers in C++ and specifically we're gonna be talking about the kind of raw style function pointers that really come from C so don't think that I'm kind of being misleading or anything like that we are going to get into more this C++ way to do function pointers in a later video along with things like lambdas which are anonymous functions kind of without a formal function definition and all that stuff we'll get into that as we go along but first I want to start off with kind of the most primitive thing which is kind of what I like to call a raw function pointer which again comes from safe so what is a function pointer so we've learned about pointers if you guys haven't seen that video it's linked up there make sure you understand what pointers are you don't necessarily need to understand how memory works or how pointers working in C++ to kind of use function pointers but you definitely do need to watch that video so definitely check it out but basically function pointers are just a way to assign a function to a variable so functions usually as we've been using them now if you don't know what a function is by the way I did make a video on that a while back so definitely check that out as well but the way that we've kind of been using functions so far has just been like as something that we call right it's not it we can't really like do any sort of logic with functions it's just like a symbol that we can call when we want something to happen and we can of course give it parameters and in turn receive something in return if we write a function that returns something out and void so that's how we've been using functions so far however you can actually assign functions to variables and like to extend from that you can also pass functions into other functions as parameters and there's a whole bunch of things that you can actually do with functions that really creates for some interesting and complex logic that would otherwise be extremely messy to do it really cleans all of a stuff up so the best way to demonstrate what a function pointer is and how to use it and what it can do is obviously by an example so let's take a look so the first thing I'm going to do is write a function and this is gonna be really simple it's gonna be a void function it's going to be called hello world and it's just gonna print hello world we're gonna start real simple here okay so hello world there we go and then what I'm going to do is of course just call it normally right that's how functions work we call it this one doesn't have any parameters so we don't need to put any parameters in and if I run my program of course I'm going to get the text hello world printing and you can see here that I do okay great perfect now let's assign this function to some kind of variable now we learn about the auto keyword and the auto keyword is one of those things that is actually really useful with things like function pointers and we'll definitely get into that in the future as well but let's just give Auto a shot and see if we can assign hello world to order some want to create a variable called function I'm going to set it equal to hello world now it's right away you can see this isn't working because we can't deduce Auto type because really hello world returns void and what we're doing here is we're actually calling the function but if we get rid of the parentheses right suddenly we're not actually calling the function we're actually getting the function pointer and specifically this would look like this with an ampersand so we're kind of getting the memory address of that function now functions are of course just CPU instructions and they're stored some way in our binary when we actually compile our code in the future we might actually take a deep dive into the binary file and see our actual CPU instructions and how all that works but for now just imagine that literally when you compile your code every single function gets compiled into CPU instructions and they are somewhere in our binary in our executable file right so what we're doing over here with this ampersand is we're saying hey in this executable file let's find this hello world function and let's get the memory address of those CPU instructions that's basically what this is right so what we're doing is we're retrieving the location of the instructions to execute when this function gets called and so now we don't actually have to use this ampersand by the way because there is an implicit conversion which we'll see in a minute so you can just do this right hello world and that's it so now what I can do is I can actually call my function like this and let's actually call it twice I'll hit f5 and you can see that I get hollow it well printing twice so that's pretty cool we've actually managed to assign a function to a variable now this is order but what type is this actually well if we hover our mouse over this look at what we get we get void and then like an asterisk and then function and some parentheses looks a bit weird so let's actually write out that type so we get void then we need to give us some kind of name in our case because we are creating a variable so before we called it function let's call it function again so in parentheses I'm putting an asterisk followed by the name of my variable and then any parameters that my function might take so if it took an integer I would put in int like that if it took a string I would put in string now we're not taking anything as you can see this is just no no parameters here so I can just kind of end it like that this is the actual type now this does look weird because this well this is ultimately the type kind of right it's just that we need to give it a name like any other variable and function just happens to be the name this can be chanted this can be anything you want it's just a name and then what we can kind of do here is just a sign churner to say hello world or something like that and you can see that it works like any other variable and you can of course do that here as well if I just write hello world that also works like that okay it does look very confusing that's gonna be step one like I've called the channel a cold shot over here like as if it was a function if I hit up five of course it still works this does look confusing I totally agree which is why what people tend to do with function pointers like this is either use author or my kind of preferred way is actually to type def at all using it so we're basically creating an alias for it so what I mean by that is instead of this kind of weird code what we can actually do is say type def then this actual type here and we can call it like our I don't know hello world function or something like that right and of course this hello world function name goes into here because that's our actual name so it's basically the type but we put a start out at the front what that now means is that if I want to actually use this I can just type in hello world function then a variable name like function equals hello world and then of course I can just call function like that okay let's make this a little bit more exciting I'm gonna add a parameter here it's gonna be an integer is gonna be called a and then at the end of hello world I'm actually going to print value and then a just like that whoops let's put this on the right side all right so now this hello well function of course we get an error here we can't assign it because hello world is a function that takes in an integer so what I'm going to do is change my function definition here to actually include an integer as a parameter and now you can see this works and when we call this function which we're doing over here we now need to specify an integer so I'm going to type in 8 and then hit f5 to run my code and you can see now I get the value 8 printing so what I've kind of done now is I have a function that I can call using a variable name like this and of course it's parameterised so I can put anything I wanted to here and it will call that function with different values which again is pretty cool so that's the that's the basics of how it works that should probably show you enough to use it in pretty much all of its capacity but now let's take a look at an actually useful example of why you might want to use function pointers in the first place so suppose that I have a vector and just include a vector over here don't delete this code as well and all of this in fact I have a vector of integers I'll call this values I'm just going to cyan't using this kind of initialize a list here like that okay and I want to create some kind of function which iterates through all of this and perform some kind of action so of course a for each loop is basically what I'm writing here but let's pretend that I wanted it to be in a function because maybe did some other things as well and that's just the example so what I'm going to do is I'm going to write a for each function I'll pull it for H it's going to take in this standard vector of integers called values and then it's going to do something so I'll just say for int value in the values and then we want to actually do something so let's go ahead and call this Oh H values now how do we tell this function what we want it to actually do for each of these values well the way we're gonna do it is just by passing in a function essentially so up here I'm going to create a function I want to call this print value it's going to take in a value and almost going to do is we print it so value and then the value very simple function right and so what I'm going to do here is inside for each I'm actually going to write one more parameter here that actually is this function so it's going to be void the name I'm going to call is func and then of course it takes one parameter which is an integer so I'm going to write in like that and then inside this for each loop I'm actually just going to write func and then call it with value as the parameter of course because that's the current iteration of the for loop and then inside is 4-h I'm passing in the values of course which is our vector and I'm also going to pass in this function which is called print value so now basically what we've got is a function into which we can pass in a vector of integers and for each of these integers inside this vector it's going to execute this function that's what's happening over here and you can see what that looks like so if I hit f5 you can see what I get here I get all these integers that I've got one five four two and three as I defined with my list here and I'm getting this print value function executed with each of these integers as a parameter so that's pretty cool it's a way that we can kind of tell our function hey I want you to do this at a particular time now this might seem a little bit overkill so I'll show you one more way to kind of do this and this will kind of late into the next video that we have so instead of kind of defining this function it's a little bit messy just have an extra function floating around especially if we only intend for it to be used inside this 4-h function so what we can do is use something called a lambda and lambda is essentially a normal function except it's not declared as a normal function like this it's kind of just declared during our code and it's kind of a throwaway function that isn't actually a real function it's an anonymous function and the way that we can kind of declare that is by using square brackets like this parenthesis like this will take you now actual parameter that we need to take in here which is out value and then with curly brackets we basically tell it what we want it to do so as if it was any other function so we'll save current value and then the actual value that we have which is going to be value which is the parameter that we passed in okay just like that pretty simple stuff let's hit f5 and you can see that I get my values printing like before but we don't have an X function we've literally just kind of written an inline these square brackets I was called CAPTCHA method so that's how we pass variables in from the outside world our parameters going here and inside and this is just like any other function body we could even run it like this if you wanted to and you can see that's the actual function okay we're gonna talk way more about lambdas in a video dedicated to lambdas we're also going to talk about something called STD function which is standard function and standard binds and all that stuff so there's a lot more to actually discuss to do with function pointers this is just kind of a gentle introduction so that you can see what they are and how we can use them and all of that and also the primitive way of how they exist in C which is just kind of like this void and then it's just the the signature is really really weird and almost no one uses this in actual C++ anymore but definitely be aware of it because you will see a lot of code like that just in your programming life anyway I hope you guys enjoyed this video if you did you can hit that like button you can help support this series by going to patreon.com/scishow Cherno there are some really cool rewards that you'll get there for helping to support this series such as getting videos early and like monthly kind of hangout sessions which we do and access to source code from all my other series and all that kind of cool stuff thank you huge thank you as always to all the patreon supporters this series would not be here without you so thank you so much I will see you guys next time goodbye [Music]
Info
Channel: The Cherno
Views: 238,348
Rating: undefined out of 5
Keywords: thecherno, thechernoproject, cherno, c++, programming, gamedev, game development, learn c++, c++ tutorial, function pointer, functions, lambda, pointer, std::function
Id: p4sDgQ-jao4
Channel Id: undefined
Length: 12min 41sec (761 seconds)
Published: Wed Jan 31 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.