Functions won’t eat arrays whole - Do this simple thing instead!
HTML-код
- Опубликовано: 11 сен 2024
- 🤩 FREE Arduino Crash Course 👇👇
bit.ly/get_Ard...
**Learn to program and prototype with Arduino now! Join our membership.**
bit.ly/3EZHSkH
**Get the code, transcript, challenges, etc for this lesson on our website**
bit.ly/3ug3ovr
**We designed this circuit board for beginners!**
Kit-On-A-Shield: amzn.to/3lfWClU
FOLLOW US ELSEWHERE
---------------------------------------------------
Facebook: / programmingelectronics...
Twitter: / progelecacademy
Website: www.programmin...
________________________________
Are you trying to figure out how to use arrays with functions in Arduino? Maybe you wanna pass an array to a function or multiple arrays to a function, and maybe you even wanna try to, like, output an array or something like that. Is that even possible with Arduino? The core difference in dealing with arrays than when dealing with other data types, called primitive data types, like integers, and floats, and bytes, and that kind of thing, is that arrays are passed by pointer, not passed by value. So if you wanna learn how to figure out how to pass an array to a function or multiple arrays then watch this video 'cause that's exactly what we're gonna dive into. Subscribe to our RUclips channel to get more videos like this. All right, so let's get started. So the first thing I wanna do is point out this function right here. I'm gonna zoom in, all right. Let's zoom in on this puppy. I got this function called add. You know, really creative here. It takes two values, val_1 and val_2, and there's, they're integer values, right. And then what do we do inside this function? We just add 'em together, and then we return the result. Now in this program, I've got num_A and num_B. Num_A is 5 and num_B is 3. And then down here in setup, I am setting the integer sum equal to the output of that add function, right, and we pass in num_A and num_B. Okay, so let's check this add function. When the add function takes those values in, val_1 and val_2, so that val_1 would be 5 and val_2 would be 3, it actually makes a copy of those values in here. So val_1 is different than num_A. It happens to be holding the same value, 5. And val_2 is different from this variable num_B. They just, you know, they copied over the value. We passed by value into this function. And, so, anything we do with val_1 and val_2 inside this function, only affects val_1 and val_2. It doesn't affect num_A and num_B. Okay, so maybe that sort of makes sense, maybe it doesn't. Okay, so that's like, you know, this add function, pretty simple, pretty straightforward. Well, let's look at a function that takes arrays as inputs. So this function is called elementwise_multiplication, and it takes three arrays of floats. So arr_1 is an array of floats, arr_2 is an array of floats, and buffer is an array of floats. And then it also takes a length variable, and then it just does some math on these arrays, all right. Namely, what it's doing is an elementwise_multiplication. And let's just take a look at these arrays real quick here. So we've got a weights array, an inputs array, and an output array. So let me just line these up real quick. That that might help a little bit. What it's gonna end up doing in our function here is it's gonna multiply the first element of this array times the first element of this array, and it's gonna save the output, that product, it's gonna save it into the corresponding element in this output array. And then it'll take this element, multiply it times this element, and save it into the next one. And then this one times this one, save it into the next one. Hopefully, that sorta kinda makes sense. And, so, if we look down here in the setup, we've got elementwise_multiplication, and we pass in weights, inputs, output, and NUM_ELEMENTS. Okay, so the primary difference here is that when we are passing an array into a function, the function does not make a copy of the values in the array. So like this array, weights right here, {0.1, 0.2, 0}, those values are not copied into this function. Instead, what's copied is a pointer to the memory address where the data begins...
CONTINUED...
bit.ly/3ug3ovr
**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.**
This channel is one of those gemstones you find on the internet, thank you for your great work!
Thank you Roberto!
Grateful you add this topic to Arduino tutorials, but I found it very important when dealing with multiple identical elements and you want to apply the function to those elements. thank you
Thanks for watching!
I was trying to pass an int array from function to function but could not get it to work, the end result would be random numbers. I guess it’s because once that function is executed, the values are erased. Is there a way to do this without initialization a global array variable?
Man! I love how you explain the concepts. All of the sudden a confusing idea becomes so familiar
Thanks Edie - I hope these are helping!
Excellent, simply put.
Glad you liked it Flashback!
"Yay, functions will make my code cleaner."
*tries to return an array in his first function*
"malloc(), free(), pointers, return by reference... well, this is BS..."
I just wanted to have a few elements neatly sorted in an array. Everything works fine with no stinky functions. Idk what's so problematic about arrays like holy fck... they might weight a bit more, but that's it. If they work everywhere but in functions, then what's the point of using those? I hope this wonderful video will fix this without wasting precious memory that people on the forums were freaking about.
EDIT: Hmm... so basically, since memory-wise arrays don't store their elements, but rather pointers to those elements, we can just forget about sending an array out of a function, because the manipulations we do, we do on the actual original elements themselfes, so everything is "already sent" so to speak. (It's as if instead of writing a letter to somebody, we write in their diary. lol)
Thanks a lot, man, finally got it.
what is the difference between your youtube lessons and your accadamy course?
Great question! Our academy has in-depth structured courses as well as a private forum. If you like our RUclips channel content, thank you would enjoy our training program.
I have just started working with Arduino a few weeks ago. I'm using buttons and the Adafruit NeoPixel library to set a certain pixel to a certain color. This is working, but obviously when I reset the Arduino, the pixels reset as well. I am trying to use micro SD card to store the specific pixels and the color I've set them to. But I can't figure out how to store the info and then read it back, so that when the Arduino starts up, the pixels light up with the correct color. I need to be able to set pixels one at a time, to red, green, or blue. Any help would be greatly appreciated.
Super appreciate these great tutorials. Got a question: Since the BUFFER array is being used to store the multiplication results does this imply that the two original arrays are left intact?
Great question Frank! You are correct.
array funtions?
Thanks executive! Updated
@@programmingelectronics Hey I am really glad that you're making these programming concept videos on Arduino. Just want to let you know, you're making a lot more of positive impact than you may notice. I learned programming using Arduino, not the best path but was stuck in there but I am really happy that you helped me get out. Keep up the great work!
@@programmingelectronics hi sir informative video on arrays concept . Can you pls do a video on how to return function pointer . And how to use understand the usage typedef with function pointer .
Thanks so much @yashaswi! It means a lot!
I'll see what we can do - thanks so much for the recommendation!