Arduino Function Pointers

🤩 How much Arduino can you learn in 7 Days? 👇
bit.ly/7_Day_Arduino_Challenge
✅ Sign up and get free $100: www.nextpcb.com/register?code...
✅ $0 for 1-4 layers PCBs: www.nextpcb.com/pcb-quote?act...
✅ PCB Assembly capabilities info:
www.nextpcb.com/pcb-assembly-...
**Learn to program and prototype with Arduino now! Join our membership.**
bit.ly/3Gt67ZA
**We designed this circuit board for beginners!**
Kit-On-A-Shield: amzn.to/3lfWClU
FOLLOW US ELSEWHERE
---------------------------------------------------
Facebook: / programmingelectronics...
Twitter: / progelecacademy
Website: www.programmingelectronics.com/
________________________________
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.**

Пікірлер: 22

  • @VClagte
    @VClagte Жыл бұрын

    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?

  • @vinothn4228
    @vinothn4228 Жыл бұрын

    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 🙄

  • @programmingelectronics

    @programmingelectronics

    Жыл бұрын

    Thanks for watching Vinoth!! No specific cpp videos yet... :)

  • @roberthatcher2773
    @roberthatcher2773 Жыл бұрын

    As always, great stuff Michael.

  • @programmingelectronics

    @programmingelectronics

    Жыл бұрын

    Thanks Robert!

  • @robertrobert5583
    @robertrobert5583 Жыл бұрын

    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.

  • @programmingelectronics

    @programmingelectronics

    Жыл бұрын

    Thanks for the note Robert! The biggest advantages I see is being able to pass function pointers as arguments to other functions, but I am sure there are other uses. For example, you might have a calculate(val_1, val_2, func_pointer) function that takes 2 values and a function pointer as arguments. The function pointer you pass in could perform an add, subtract, multiply, on those two numbers.

  • @flashback9966
    @flashback9966 Жыл бұрын

    Very useful, thank you.

  • @programmingelectronics

    @programmingelectronics

    Жыл бұрын

    Great to hear that - thanks for the note!

  • @TOMTOM-nh3nl
    @TOMTOM-nh3nl Жыл бұрын

    Thank You

  • @programmingelectronics

    @programmingelectronics

    Жыл бұрын

    You're welcome Tom! Thanks a ton for watching!!

  • @user-gw1pj7fh7q
    @user-gw1pj7fh7q5 ай бұрын

    Класс 👍

  • @programmingelectronics

    @programmingelectronics

    5 ай бұрын

    Thanks!

  • @user-gw1pj7fh7q

    @user-gw1pj7fh7q

    5 ай бұрын

    @@programmingelectronics спасибо за вашу работу , это действительно доходчиво даже мне не понемающего иностранный язык понимать язык си спасибо вам большое !

  • @cemilerdem5694
    @cemilerdem56945 ай бұрын

    Thanks so easly

  • @bernym4047
    @bernym4047 Жыл бұрын

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

  • @programmingelectronics

    @programmingelectronics

    Жыл бұрын

    That is about right Berny!

  • @FreddyBNL
    @FreddyBNL Жыл бұрын

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

  • @programmingelectronics

    @programmingelectronics

    Жыл бұрын

    Sorry about that Fred! We do have the Kit-on-a-Shield in stock, just needed to update the inventory number. Should show up shortly.

  • @FreddyBNL

    @FreddyBNL

    Жыл бұрын

    @@programmingelectronics ok, thnx 4 the quick respons 👍

  • @programmingelectronics

    @programmingelectronics

    Жыл бұрын

    Happy to help!

  • @sujinak5743
    @sujinak5743 Жыл бұрын

    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