Arduino Function Pointers #Arduino #C #C++

preview_player
Показать описание
🤩 FREE Arduino Crash Course 👇👇

✅ PCB Assembly capabilities info:

***Learn to program and prototype with Arduino now! Join our membership.***

***We designed this circuit board for beginners!***

FOLLOW US ELSEWHERE
---------------------------------------------------
________________________________
So, have you ever written your own function? I really hope so, because it's pretty fun and it's super useful to know how to write your own functions. Functions 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 you're watching this series of lessons, I'll assume that you're familiar with functions, and this is extremely important because we're gonna be talking about function pointers, and if you're 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 we're gonna cover is what a function pointer is, the anatomy of a function pointer, and how to point a function pointer. All right, so what the heck is a function pointer? Well, a function pointer is just like the pointers that we've 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 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 they're 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 what's cool is that we can use function pointers just like we can use other pointers. We can store them in a raise, we can pass them to functions, and we can even return them from functions. What's really interesting to learn is that every time you create a function, you're 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, here's an example of what it looks like. A little bit complex, it might feel like you're 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 anytime you create a variable. You always have to start with the data type, right? Well, it's no different for a function pointer. We're 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.

***About Us:***
This Arduino lesson was created by Programming Electronics Academy. We are an online education company who seeks to help people learn about electronics and programming through the ubiquitous Arduino development board.

***We have no affiliation whatsoever with Arduino LLC, other than we think they are cool.***
Рекомендации по теме
Комментарии
Автор

Great explanation of how Function Pointers work, but I'm wondering WHY you would want to use this instead of just calling the funtion. Does it make the compiled code faster, or is it just a sort of shorthand for getting the same result?

VClagte
Автор

Nice to watch. I have no doubts on functions, but i just come to know about your teaching skills 😁... ! Have you posted any cpp videos 🙄

vinothn
Автор

I maybe guessing here but it looks like you are using a function in more than one different location with different variables and arguments?

bernym
Автор

This is very interesting and well explained. I write my own functions all the time having learned how to do so from you. I'm curious to understand what advantages pointer functions have. Looking forward to the next instalment.

robertrobert
Автор

Link to the Amazon product page says: "Currently unavailable
We do not know if or when this product will be available again."

FreddyBNL
Автор

Bro my arduino uno is is not detecting in my pc and also the orange led is also not blinking. When i press the reset button nothing is happening. What is the problem in the board

sujinak