 Hi guys! In this video, we're going to be taking a look at what happens when we pass arrays to functions. So first, let's just take a look at a simple example that involves passing an array to a function, performing some operation and returning a value. So here we have a function called sum, which takes as its formal parameter an integer array data. It then adds together all of the items in data and returns the final sum of all of the items. Now you can see up here in setup, we have an integer array a, and we then call the sum method below here, passing in a as the actual parameter and then storing the returned result from our sum function in b. So here data is the formal parameter of the sum function and for this function call a is the actual parameter. Now if we go ahead and run this with this print statement here, you can see we print out 190, which is the sum of all of the items in a, 10, 70, 20 and 90. Notice though, let's say I try to print out result or I try to print out data, 0. I'm going to get this red squiggly line here and the error message, the variable data does not exist. And the reason for this is that data and result only exist within the scope of this sum function. So we have setup and then within the scope of setup we have a, which references an array containing four values like sir, and then we have b. So when this line of code is run and the sum function is called from setup, we have the sum function called with actual parameter a. And within the scope of this function call exists our formal parameter data, which refers to the same instance as a. So just to reiterate, when we call the sum function here, the formal parameter data is linked to the actual parameter a. In exactly the same way as if data were a variable that we initialized, for example, by saying integer array data equals a. The only difference being that in this case, because data is a formal parameter of our sum function here, data only exists within the scope of the function and it can't be referred to outside of some. But both a and data are variables that refer to the same array instance. So these arrows can be read as references. That is a references this instance and data references the same instance. And now moving on within the scope of some, we also have the variable result whose final value for this function call will be 190. And the value 190 is then returned to setup and stored in variable b. So now we've exited the sum function. Everything local to some no longer exists, which means this relationship no longer exists. Which is why when I tried to change earlier this print statement to data zero, we ended up getting that error message because data does not exist within the scope of setup only within the scope of some. Now there are a couple really important points to be aware of when passing arrays to functions. And the first is this. So when passing an array to a function, if you modify the contents of the formal parameter, the contents of the actual parameter are also modified. All right, so let's take a look at what this means using this reset function just over here in processing. So just uncomment that. All right, so what this function does is it takes the as its formal parameter integer array data, and it sets each item in data to zero. All right, so let's just go back up to our setup here. I'll remove these and we'll go ahead and call reset on our array a above. And then I'm just going to write a for loop that's going to print out every item in a after reset has been called on it. So for int i equals zero, i is less than a dot length i plus plus. And we're going to say print a. All right, so now let's go ahead and run this. So we've called reset on a and now we're going to print out every item in a. And you can see I'll just expand this a little bit probably as you expected. You can see each item in a now equals zero. So by modifying the contents of data, I've also modified the contents of a because just like we saw with our previous some method. Both data and a refer to the same array instance. So by modifying the contents of our formal parameter data, we also modified the content contents of our actual parameter a. Now the second important thing to note when passing arrays to functions is this. When passing an array to a function, if you re reference the formal parameter, the actual parameter is not re referenced. So let's pop back over to processing and we'll take a look at what this means using our final function here set to null. So we have here the function set to null. It again just takes one formal parameter data. And all that it does is it sets integer array data to null. So now back up here in setup just underneath, let's create a new array X and integer array that contains three values. 10, 70 and 20. So if we just scroll down here, you can see I've drawn this is what X looks like in our notation. It refers to an array instance containing these three values. So let's now go back up to setup and write a statement here underneath that calls the set to null function. With X as the actual parameter. All right, so at the start of this set to null call data at the formal parameter data here will refer to the same array instance as X. So in our notation, it just looks like this. So let's now just go ahead and move this print statement here to underneath our set to null call. And I'm just going to change it to print out every item in X instead of a. So now if we print out all of the items in X after running set to null with X is the actual parameter. You can see here that the contents of X remains unchanged. It's still 10, 70, 20. So what's happened here is that after set to null is called the formal parameter data is set to null. So over here, you can see data no longer refers to this array instance. This relationship no longer exists, but X here remains unchanged and X's relationship to this array instance remains the same. So X still refers to this array, even though data no longer does. So that's just an example of how if we reference going back to this, if we reference the formal parameter data in this case, the actual parameter X is not re-referenced. So that is pretty much it for this video on passing arrays to functions. So I hope you found it helpful and thank you for watching. Bye.