Help your code be more readable. They reduce redundant code and functions are important to understand. As you begin to write larger and more complex programs, if youre watching this series of lessons Ill assume that youre familiar with functions – and this is extremely important because were going to be talking about function, pointers and if youre not deeply familiar with how to write your Own functions, then, it might be pretty tough to follow along with these lessons now, if you need a refresher on how to write a function, make sure to check out the Arduino course for absolute beginners, section on user defined functions. So in this lesson, what were going to cover is what a function pointer is the anatomy of a function pointer and how to point a function pointer. This video video is sponsored by next PCB with next PCB. You can get a totally free, PCB prototype. That means for zero dollars. You can get a one to four layer PCB its simple to get an instant quote. All you have to do is upload your Gerber file, Music, again, thats zero dollars for five to ten PCB prototypes. In addition, when you sign up, new customers will get a hundred dollar coupon for free thats, a hundred dollar coupon for your next PCB order for free. So if you want to go grab your five to ten free pcbs check the link in the description again get your five to ten free pcbs check the link in the description for next PCB to grab this great offer all right.

So what the heck is a function pointer, well, a function pointer is just like the pointers that weve talked about so far, except that, instead of pointing to the memory address of a variable, it points to the memory address where the instructions of a function are located. All right consider this function called add. This add function takes two integers as arguments. It adds them together and Returns the sum as an integer. So to accomplish this task. Many lower level instructions have to be executed by the processor, and those instructions are generated from our code by the compiler and theyre stored at a specific location in memory and the address where those instructions start is what is stored in a function pointer. So let me say that one more time when you create a function, the instructions for that function are saved in memory. The first instruction, like all the instructions will have an address. That first address is what we will store in a function pointer, so a function pointer is going to be a variable that holds the memory address of function, instructions and whats cool is that we can use function, pointers just like we can use other pointers. We can store them in arrays, we can pass them to functions and we can even return them from functions. Whats really interesting to learn is that every time you create a function, youre actually creating a function pointer with the same name as your function.

That is the function. Name add holds the address of where the instructions for the add function starts. So how do we create a function? Pointer, well, heres an example of what it looks like a little bit complex. It might feel like youre looking at the Whiteboard at some NASA research and development laboratory, but this is how it works. The first thing we need to specify is the data type that the function will be returning so think about any time you create a variable. You always have to start with the data type right. Well, its no different for a function pointer were specifying the data type that this function will return. The next part, in parentheses, is going to be the name of the function pointer, preceded by the asterisk and inside parentheses. So all of that is important. We need to have the parentheses we need to have the asterisk, and then we need to have the name of the function pointer. The next part is a comma separated list of the parameters that the function takes. So if the function takes two integer parameters, we would have int comma int. If the function took three float, parameters wed have float, comma float, comma float. If it takes a byte. All we would have is the parentheses byte, so yes sort of complex, but once you get the syntax down its not too bad. So this example that we have here is creating a function pointer named Operation.

The function it will point at is going to return an integer value and the function it will point at is going to accept two integers as parameters. So how can we actually point this function? Pointer well, lets revisit that add function, we talked about. Add takes two integers as its arguments. You know the parameters that it requires and its going to return an integer. So this is a good match. Our operation pointer could point to this add function. So how do we actually Point operation at this? Add function well its as simple as setting operation equal to add. Now you might be like hold on a second shouldnt. It be operation equal to Ampersand, add shouldnt! We be using that reference operator in front of add to get the address of add. Well, we actually dont have to do that, because the compiler inherently uses the functions address when its name is referenced. So now, with these lines of code operation is pointed to add. Now what gets really interesting is that we can use operation just like we could use. Add so see, weve got here operation and were passing it two arguments 14 and 6., and since operation is pointing to the add function, it would return the sum of those two values. Okay, I know thats kind of like mind, blown here, youre, probably half thinking. This is pretty cool and half thinking like how are you ever going to use something like this and maybe youre a third thinking about ice cream or something like that or maybe thats what Im thinking about, but so were going to be covering some use cases of Function, pointers and following lessons, but before we get there lets cover another example of creating a function pointer before we call it quits.

So here weve got a function called display. Vowel display Val doesnt return any values, hence weve got the void at the beginning, and it takes one integer argument called Val and then it just prints off the value that you pass to it and what weve done is created a function pointer named demo. The function that it points to must not return any values, hence the void at the beginning and its going to take a single integer as its parameter and what weve done is point a demo at the function display vowel and now we can use demo just like We would display Val, if youre, really enjoying this Arduino programming stuff, make sure to check out our website. Programmingelectronics.Com, we have a training program that can really help get you up to speed, pretty darn, quick learning how to program with Arduino. Also before you go make sure to subscribe to the channel, and if you enjoyed the video, please do like it. If youve got some questions, leave them in the comments. We do read them all and try to do our best to answer them. Thanks a ton have a great one bye. This lesson is part of a course at programming Electronics Academy, if youd like to get access to the full course and all the materials related to this training check out programming Electronics Academy, where you can get access to all of these lessons and all of the coursework.

https://www.youtube.com/watch?v=tlh3_77E-sU