Functions won’t eat arrays whole - Do this simple thing instead!

Programming Electronics Academy
Programming Electronics Academy
11.5 هزار بار بازدید - 2 سال پیش - 🤩 FREE Arduino Crash Course
🤩 FREE Arduino Crash Course 👇👇
https://bit.ly/get_Arduino_skills

**Learn to program and prototype with Arduino now! Join our membership.**
https://bit.ly/3EZHSkH

**Get the code, transcript, challenges, etc for this lesson on our website**
https://bit.ly/3ug3ovr

**We designed this circuit board for beginners!**
Kit-On-A-Shield: https://amzn.to/3lfWClU


FOLLOW US ELSEWHERE
---------------------------------------------------
Facebook: Facebook: ProgrammingElectronicsAcademy
Twitter: Twitter: ProgElecAcademy
Website: https://www.programmingelectronics.com/
________________________________
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 YouTube 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...
https://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.**
2 سال پیش در تاریخ 1401/09/04 منتشر شده است.
11,550 بـار بازدید شده
... بیشتر