Pass Array to Function in Arduino
A transcript to our lesson:
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. 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. 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. What’s 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, it’s going to change this actual array right here. It’s not gonna act on a copy of those values, it’s gonna act on the actual array. And that is because when we pass in an array, it gets passed by pointer. So it’s the pointer that gets copied into the function, not the data. So this kinda 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 that’s kind of what we’re doing right here in this elementwise_multiplication function. So up here I declared an array named output of length NUM_ELEMENTS, which is 3, and I’m passing that to this elementwise_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 what’s 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, I’m calling the function elementwise_multiplication. I pass in weights, inputs, output, and NUM_ELEMENTS. And if I were to print output after I call this function, I am going to see the product of these inside output. So lemme go ahead and do that, just so you believe me, but let me do that real quick, and then we’ll see. 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 X 0.1 would be 0.85. 0.2 X 0.65 would be 0.3. Zero times anythings gonna be zero. All right, so hopefully, that makes a little bit of sense. I hope this was helpful. Again, there’s a lotta details on memory addresses, and where the stuff’s getting stored, and all that kind of stuff, that can be, you know, can really dive down into the weeds. Again, if you’re interested in that, check out programmingelectronics.com. And if you’re 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 of your day. Bye.
