 Welcome back, in the previous segment we discussed what how a name of index is interpreted, basically we discussed the semantics of the square bracket operator. In this next segment we are going to talk about how arrays are passed to functions. So we really would like to have functions on arrays, I mean we might want to do the following things, you might say want a function which finds the largest value in an array or if you might want a function which decides whether a given value is present in an array or a function which calculates the average of the elements in an array and so many other things could be done. So in this segment we are going to see how all of these things can be done. Let me just plunge into it directly by giving you an example. So here is a program which finds the average of elements in an array or a function rather, so this is the function and this is the main program which calls this function. So before looking at it too carefully and trying to see how it executes, let us first make some preliminary checks. Let us first check if this is a syntactically valid program, never mind what it does. So what do I mean by that? Well when we make a function call, so the types of the arguments must match the types of the parameters to the function. So is that happening here? Well the first parameter to average has type double star. So this is the first parameter and its type is double star. The first argument in the call is Q and its type if you remember is also double star. Why? Because it is the name of an array of doubles and its value is the address of the zeroth element and it points to a double element. So the type is correct, the type of this is indeed the same as the type of this. So when we execute the function, the value Q will indeed be copied into M, so no problem with that. The second parameter here is type n and here also is type n, so that is of course absolutely no problem about that, so there is nothing new about that. Now let us see how this is going to be executed. So when we execute this average of Q4, what is going to happen? Well activation frame will be created for average and the value of Q which is the starting address of this array will get copied into the parameter M. And in each iteration what is going to happen, Mi is needed. Now like the example that we took a little bit earlier, this M has exactly the same value as this Q. So if we write M of i in this, even in this M of i is going to mean the same variable as Q of i because M and Q mean the same thing, M and Q have the same value. So M has the value starting address of Q of type double, so M of i means variable of type double at address Q plus 4i or otherwise it is the same thing as Q of i. Exactly like the example that we just did in the previous segment. So effectively what is going to happen is that we are going to calculate the average of Q0, Q1, Q2, Q3 which is exactly what we want. And so this will indeed return, it will calculate the sum of the elements and then return divided by 4, so it will actually return the average exactly like we wanted. And the main program will print it out, so that is how the execution will go. So how are arrays then passed to functions from this example? So we are passing two arguments, the starting of the address of the array and let us say the corresponding parameter is A and the number of elements in the array or maybe I should have written M to keep consistency with the previous example but let us say it is A. So now can elements of the array be accessed in the call function? Well, if you write A of i in the call function, it will actually give you the ith element of the calling program and therefore you can access the elements. Even though you are not copying the elements, you are just sending the starting address of that array. But if the function modifies the elements, then the modifications will be seen in the main program because the function is directly operating with the same array, it has a pointer and that pointer is operating directly in the activation frame of the calling program. So when you pass the name of an array in a function call, its value, the starting address of the array is copied to the corresponding parameter but because we passed the starting address of the array, we are effectively enabling the function to pass the array. You can say the following about this entire process, you can say that the array name is passed by value. So let me just explain this. So in this, this array name is passed by value, why? Because the value of q is put in m and so therefore m and q have the same value. But indirectly because you are passing the array name, you are allowing the call function access to the entire array. So you could say that the array elements are passed by a pointer, well effectively you are saying that all the array elements are passed by a pointer to the zeroth element because if you add the appropriate displacement, the called function can get to every element which it did in our average function and it can do in any old function. The interesting part in all of this is the square bracket operator. So it given the address of any array and an index, it can get us to the corresponding element even if the address belongs to a different activation frame. Now when we write functions and alternate syntax is also allowed and maybe it is recommended. So I can write the average function as not double star m, but double m square brackets. In this double m square brackets is synonymous with double star m, it means the same thing. But it is slightly more indicative that we expect m to be an array name and not just any old pointer. And this is really for our benefit, c++ treats both of these things as essentially the same. And I should further note that this m square bracket, open square bracket close syntax is not allowed in other places besides the declaration or definition of a function. Now the second argument over here is not really linked formally to this element, it is just a second argument, it is just another integer. What you do with that integer inside the function body is your problem. So we are choosing, we are deciding when we write this function that the second argument is supposed to be the length of the array. So of course the average function does not know that. So you can in fact pass a smaller value of n and then the average function will just take the average of the initial so many elements rather than all the elements in the array. So what have we discussed? So we have said that no special mechanisms are needed to pass an array to a function other than the semantics of the square bracket operator. And also the idea that the array name is a pointer to the starting address of the region allocated for the array. We pass a pointer to the zeroth element that is we state where the array begins in memory and we pass the number of elements. This is what we do if we want to pass an array to a function. The square operator can be used to access individual elements of the array and this concludes this segment. In the next segment we are going to build a slightly more elaborate function, a function to sort an array but we will take a quick break.