Pass Arrays to Functions in Arduino
So if you want to learn how to figure out how to pass an array to a function or multiple arrays, then watch this video because thats exactly what were going to dive into foreign subscribe to our YouTube channel. To get more videos like this, alright, so lets get started. So the first thing I want to do is point out this function right here, Im going to zoom in all right lets, zoom in on this puppy. I got this function called add. You know really uh creative here. It takes two values: Val one and Val two and theres their integer values right and then what do we do inside this function? We just add them together and then we return the result. Now, in this program, Ive got num a and num B num a is five and num B is three 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 lets check this add function when the add function takes those values in Val 1 and Val 2, so that vowel one would be five and Val 2 would be three. It actually makes a copy of those values in here. So vowel one is different than num a its just. It happens to be holding the same value. Five 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 two.
It doesnt affect num a and numpy okay, so maybe that sort of makes sense, maybe it doesnt. Okay, so thats, like you know this add function. Pretty simple, pretty straightforward! Well, lets look at a function that takes arrays as inputs, so this function is called element wise multiplication and it takes three arrays of floats so array. One is an array of floats array. Two 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 its doing is an element wise multiplication and lets. Just take a look at these uh arrays real quick here, so weve got awaits array and inputs array and an output array. So let me just line these up real quick that that might help a little bit what its going to end up doing in our function. Here is its going to multiply the first element of this array times the first element of this array and its going to save the output that product its going to save it into the corresponding element in this output array and then itll. Take this element multiply it times this element and save it into the next one, and then this one times this one saving the next one. Hopefully, that sort of kind of makes sense, and so, if we look down here in the setup, weve got alt element wise 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.10.20, those values are not copied into this function. Instead, whats copied is a pointer to the memory address where the data begins. Now you might be thinking to yourself like what are you talking about memory locations? All this crazy stuff, listen man! I just need to know how to use arrays and functions. Whats, the Jazz. Okay, I totally get you there now. If you are interested in those finer details, we do have a course on using pointers and deep diving into this kind of thing. So you can check that out at your convenience. But the bottom line is this: when you pass an array to a function – and you do something to the array in the function, then the actual array itself that you passed in is going to change right. So if you pass in weights to a function – and you start like multiplying, you know the elements in here times something its going to change this actual array right here, its not going to act on a copy of those values, its going to act on the actual Array – and that is because, when we pass in an array it gets passed by pointer, so its the pointer that gets copied into the function, not the data, so this kind of leads us into.
How can we get an array returned from a function? Well, the shorter answer is, you cannot return arrays from functions? The best you can do is return a pointer to an array from a function, but what you can do is pass a function like a quote: unquote, storage array or an output array or a buffer, and then that function will store your quote. Unquote, output in that array and thats kind of what were doing right here in this element wise multiplication function. So up here I declared an array named output of length, num Elements which is three and Im passing that to this element, wise multiplication function and what I do inside the function is I update that array so here we refer to it as buffer, but you can See buffer is getting updated and whats happening, since we are passing by pointer, is that my output array is now getting updated. So let me shrink this code. A little bit try to get more of it on the screen so down here in setup, Im calling the function element wise multiplication. I pass in weights inputs, output and num elements and if I were to print output after I call this function Im going to see the product of these inside output. So let me go ahead and do that just so. You believe me, but let me do that. Real quick and then well see good morning all right, so I threw in a little function here to print arrays and you can see when we print it before we get all zeros when we print it after we get the product of these right so 8.
5 times 0.1 would be 0.85 0.2 times. 0.65 would be 0.3 0 times. Anything is going to be zero all right, so hopefully that makes a little bit of sense. I hope this was helpful again theres a lot of details on memory addresses and where the stuffs getting stored and all that kind of stuff that can be, you know, can really dive down into the weeds again, if youre interested in that check out programmingelectronics.com and if Youre interested in Arduino in general make sure to subscribe to the channel. We put out videos like this all the time. Hopefully this is helpful for you. If you need some questions clarified. Please just ask in the comments. I really do my best to help answer those as best I can, or at least help point you in the right direction, thanks a ton and have a awesome rest your day, bye.