 Hi guys, so in this video we're going to be taking a look at what happens when we pass arrays to functions or methods in Java. So first let's just take a look at a simple example that involves passing an array to a method performing some operation and returning a value. So here we have a method sum, which takes as its form formal parameter and integer array data. It then adds together all of the items in data and returns the final sum of all of the items. Then in our main method up here we have the integer array A and we then call the sum method here passing in A as the actual parameter. And then we store the returned result from our sum method in B. So here data is the formal parameter of the sum method and for this method call A is the actual parameter. Now if we go ahead and run the main method with this print statement here, you can see that we print out 190 which is the sum of 10, 70, 20 and 19. 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 I'm going to get the error message data cannot be resolved to a variable. And the reason for this is that data and result only exist within the scope of this sum method. So we have our main method and then within the scope of main we have A which references an array containing four values like sir and we have B. So when this line of code is run and the sum method is called from the main method, we have the sum method called with actual parameter A. And within the scope of this method call exists our formal parameter data which refers to the same array as A. And we also have our variable result whose value changes throughout the run of the for loop like so until the loop terminates and results final value 190 is returned to the main method and stored in variable B. So once we've exited our sum method, everything that is local to the method no longer exists and therefore data no longer refers to this array. So this relationship no longer exists and that's why when we tried to print out data or result up in our main method here we got that error message. Now there are a couple of really important points to be aware of when passing arrays to methods and the first one is this. When passing an array to a method, if you modify the contents of the formal parameter, the contents of the actual parameter are also modified. So let's take a look at what this means using this reset method over here in Eclipse. So what this method does is it takes the formal parameter integer array data and it sets each item in data to zero. Now just a reminder that at present, if we go back to our array A here, A references an array containing these four values here. So at the moment A references an array 10, 70, 20, 90. Now let's just get rid of our sum method here. And what we're going to do is we're going to call reset method on our array A. And I'm then just going to use a loop to print out each item in array A after reset has been called on the array less than A dot length I plus plus and print out each item in A. So now if I go ahead and run this, we can see that each item in the array referenced by A has been reset to zero. So by modifying the contents of data, we've also modified the contents of A since they both refer to the same array. So in other words, by modifying the contents of a formal parameter, we've also modified the contents of our actual parameter A. So now the second important thing to note when dealing with passing arrays to a method is if you re-reference the formal parameter, the actual parameter is not re-referenced. So let's pop back over to Eclipse and we'll take a look at what this means using our final method set to null. So we have the set to null method here. Again, it just takes one formal parameter integer array data and it simply sets data to null. So now let's go back up here to our main method and we're going to create a new array X that contains three values, 10, 70, and 20. So this is what X looks like in our notation. It references an array containing these three values. So let's write a statement here to run the set to null method with X as the actual parameter. So at the start of the set to null method when it runs with X as the actual parameter, data from set to null will refer to the same array as X. But let's now move this print statement down here and change it to print out the contents of X really quickly. So if we now print out the contents of X after running set to null with X as the actual parameter, let's go ahead and do that. And we can see here that the contents of X have remained unchanged. We've printed out 10, 70, 20. So what's happened here is that after the set to null method is called, data has been set to null. Data, the formal parameter here, or here. And so data here no longer refers to this array. So this relationship no longer exists. Data has been set to null. But X here remains unchanged. So that's just an example of how if we re-reference the formal parameter data in this case, the actual parameter X is not re-referenced. All right, so that is all for this video on passing arrays to functions or methods. I hope you found it helpful and thank you for watching. Bye.