 to go over question 4 from the Compound Data 2 workshop in this video. So question 4 is looking at what happens when you pass arrays in to functions as parameters. We've had a look at passing in primitive variables of primitive data type into functions, and we know that when we pass in a variable, it creates a local copy of that variable. So if you have variables with the same name, we had a look at this in Functions Week where if you've got two variables of the same name with processing, whenever you refer to that variable you're referring to the most local one in the most local scope. But with arrays it's a little bit different because when you pass in arrays as parameters, it's creating a shallow copy. Any changes you make to the formal parameter will also impact the actual parameter. So you want to be really careful of that, especially if you're not meaning to make changes to the actual array that you've passed in. So if you don't want to make changes to this array here, that means that you don't want to make any changes to A because they're referring to the same place. So if you have a look here, I've created that memory diagram over there on the right, and we're assuming this is run in setup, or draw, either one's fine, probably setup. And here I've got an integer called result, which is equal to the output or the return value of calling this function with this parameter. So we're calling this function, this is our actual parameter. And that's being passed into this function where the parameter name here is A. And so that is like saying integer array A is equal to R. And we know when we do that, that we're saying A is equal to the same memory reference that R has, and it's referring to this array here. So after when I enter this function, what's happening is I'm not creating a deep copy, I'm creating a shallow copy. So A is also referring to this array here, which isn't too big of a deal with this function here because we're not making any changes to the array, but it's just something to be mindful of, especially when we get to 4.2 and we design our own function. So we want to work out what result is equal to once this code is executed. First of all, we've got C is equal to zero. So I'll add that. And then I have this loop, and I recognize this loop. It's a loop that will go through each item in an array where I is referring to the index. So we don't have to use a logic table for this one because we can see what it's doing. We can just go item by item in our array starting at index zero. So for each item in our array, we have this condition that's being checked, which says AI is equal to zero. And if AI is equal to zero, then we're adding one to C. So we're starting at index zero, which is this value here. So that's like saying is six equal to zero, and that's false. So this condition, we won't be entering that condition. So we'll skip over it. And that means we get to the end of our loop and we'll move to our next index. So now the item we are looking at is the one at index zero, which is zero. Sorry, the one at index one, which is zero. So now I have a condition that says if, well, it's the same condition. AI is equal to zero. And we're at one. So at this point, we have zero is equal to zero, which is true. So we're plusing one to C. So we can plus one here, zero plus one, we get one. We get to the end of our loop, which means we increment AI. And now we're looking at the next index, which is this one here. So we can see just after the first few traces of this loop that what's happening is that we are searching through one by one through all the items in our array. And we're counting the number of zeros that are in this array, because we have a condition that says if the item is zero, then add one to our counter. And then we can see down the bottom here that once we've finished that loop, once this loop terminates, which is when we get to this item here, once we've finished with that item, our i value will be equal to 12. And 12 is not less than a dot length. So when our loop terminates, we're just returning C, which should contain the number of zeros that there are in our array. So rather than tracing that, we can quickly speed through this and say, okay, there's one, two, three, four, five, six zeros in this array. So after that loop terminates, we should have C is equal to six. And then we are returning C, which is six. So that's returning back to our function core, which is here. So in the end result will be equal to six. So we've got result is equal to six. And we've exited from that function. We've exited the scope of that function, which means any variables that were created in that scope, also we don't have access to anymore. So that means that we want to remove A. We can't access A anymore. It was only there when we were inside that function and also C, because C was created inside this function. So once all of that code is executed, except for all of the highlighting I did, I'll just remove that. This is what your memory diagram should look like. So that's a little look into what happens when you're passing parameters in, when you're passing arrays in as parameters into functions. And question 4.2 is asking you to practice writing some functions. So we've got processing up here to have a look at this question. So let's have a look first at the function that we want to design because we're joining two topics here. We're looking at functions and compound data. So write a function that when passed an array containing floating point values, doubles each item of the array. And remember that modifying the formal array inside a function modifies the actual array passed. Okay, so, okay. First of all, let's have a look at the skeleton of a function. That's what our function will look like. I'll just put void setup down here. We need to think of a name. Probably a meaningful name. And the purpose of this function is to double each item in the array. So maybe we can call it double items, something like that. Write a function that when passed an array of floating point values. So we're being passed in an array of floating point values. So that will be our parameter, float array data. And we're doubling all of the items of the array being passed. So we're not returning anything because we're just making updates to this array here. So let's create an array that we can use as a call. So we've got float array a is equal to, let's just add some floats. There we go. So when I call this function and I pass in this actual parameter here, what should be returned? Well, actually, sorry, nothing is being returned. It's just doubling. It's having an effect. And the effect is that each of the items inside the passed array are being doubled. Okay, so now we need to have a look at what we're doing here. So we know that when we're passing in this actual parameter that this formal parameter is referring to the same place in memory. So any changes we make to data will also be made to a. It's pointing to the same place. So what I can do is I can create a loop that will loop through each value inside data and double its value. It's good not to hard code the length for our condition. And so for each value in data, we want to times that by two. And then when we get to the end of this function, we'll just jump back and that will be done. So let's double check that this is doing what we want it to be doing. We'll print out our original array here. And then we'll print it out a second time once we've called this function. So this should be impacting a. And if we have a look here, we've printed out a twice once before the function was called once after, and we've successfully doubled each item in our array, which is great. And we know that that's going to work because we're passing an a into our function. So data is referring to the same place in memory. Great. But say if we didn't want to do that, say if we didn't want to impact a at all, we wanted to return a brand new array without a being impacted, but we want to return a but a doubled perhaps we want to create a new function that returns a float array return doubled array. So very similar structure except we've got a return type this time. So this time we don't want to be impacting our array. So let's create a new one. Let's call it B. And I'll just copy this over and change some values. So this is my new array here. And I'm going to pass that in to return doubled array. So in this case, because I've got a return type of returning a float array, which is a copy of B with all the values doubled, I'm assigning the output or the return value of this function call to a new float array double. B. Okay. So let's create this function here just as an exercise. So in this case, different to this function here, we know that we don't want to change the values in data, which in this case is B. So we'll need to create a new array. So float array, I'm going to call it temp because it's temporary reference is equal to a new float array with data dot links as its size. And then we want that same loop to loop through each item in temp. And then for each value in temp, I want it to be equal to data I times two. So now I'm not making any changes to data. It's being untouched, which is good. That's what we want in this case. We're just having a look at the items and assigning double of that item to this new array called temp. And then at the end, I want to return that reference temp. Okay. So let's have a look at that. I'll remove these print line statements. So if I print B, so B is the first thing I'm printing that help us out. And we'll print out B a second time after this function has been called just to make sure that we haven't changed B at all. And then again, we will do the same thing, but we'll print out double B. Okay. So let's run this code. So we can see here that we've printed out B before and after this function has been called. But no changes have been made to B, which is great. That's what we wanted. And we can see that double B is B, but every value multiplied by two, which is exactly what we wanted. So we've strayed a bit from the original question there, but it's a good example of the two different ways that you can modify an array depending on what you want. If you want to modify the actual parameter, or if you want the actual parameter to be untouched, then you'll want to create a method that looks like this.