 So, in the last two segments, we have discussed various aspects of functions. We began by saying where function should be defined and used and then we talked about how they execute, how they can have their own variables and then finally we said how should we be thinking about functions. So basically we should not be thinking about how a function executes once we have designed and made sure that it works correctly. From that point onwards, we should worry about how we use it. So we should not, when we make a call to a function, we should just trust that the function is going to return as the correct value so long as we are following the specification. In this segment, we are going to discuss something called reference parameters and let me begin by saying why these are necessary. So what we have so far has some shortcomings. So we are not able to write functions to do the following things. So for example, we cannot write a function that exchanges the values of two variables. So you may think that often we might want to say look exchange the values of these two variables. So, shouldn't we have a function to do that? Yeah, it seems like a reasonable requirement but you cannot do that using what I have just taught you. So we will see that in a minute and we will also see how to fix it. Similarly, sometimes you might want a function which does not produce just one result but maybe two results. So suppose you want to produce polar coordinates given Cartesian coordinates. So given x and y, I want to produce r and theta. So the return statement enables you to return one value. So how do you return two values? So that is another problem that we will see how to fix. So let us come to the problem of exchanging two values. So the first attempt. So here is a potential function that I could write, a possible function that I could write. So the function could be called exchange and it takes two parameters or the call will have two arguments m and n, the values that I want to exchange. So I place the value from m into temp, then I place n into m and then I place n into temp. So at this point, clearly the values of m and n are exchanged and I am not expecting to return anything and presumably in the main program I can do something like this. So I might have a equal to 1 and b equal to 2, I exchange a, b and when I print out I should expect that hopefully a will be 2 and b will be 1. However, this does not work, 1 and 2 will get printed over here and let us see why that happens. So when exchange is called, 1 and 2 are indeed placed into m and n. So our usual rule is that when we make a call the values of the arguments, 1 and 2 are copied into m and n. Now execution of exchange does exchange values of m and n. But unfortunately the change in m and n happens in the activation frame of exchange and this does not change anything over here in the activation frame of the main program. So when you return the main program has the same values and so 1 and 2 will get printed. Now how do you fix this? It turns out that the fix is actually very compact, very slight. So it is just a matter of adding two characters but they are kind of important characters. So let us see that. So these are the two ands that you have to add. If you add those two ands then this code will do what you expect it to do. So let me explain what these ands do and why they produce the behavior that we wanted. So if I write an and before the name of the parameter then it means the following things. It says then you create the activation frame for this function. Do not allocate space for this parameter but instead make this name refer to the variable, the corresponding variable from the calling program. So in this case this means that this m there will be no variable allocated for m in the activation frame of exchange but instead m will refer to the corresponding argument. So a, so whatever you do with m in this will actually be done using a. So m is just going to be an alternate name or a reference for this a. So that is why this is called a reference parameter. So m will really refer to a, m will not have the value of a but m will refer to a and n is also a similar variable. So n will also refer to b and it will not be a new value. So when this exchange call happens nothing is actually copied. So instead these parameters are thought of as referring to the variables in the calling program. So now when the function changes m and m it is really changing a and b because everything over here, so these m's and n's over here are really a's and b's. So they are just alternate names. So if I say put an and over here this is just an alternate name for whatever name you had in the call. So such parameters are called reference parameters and in fact in this case what you expect 2 and 1 will get printed. If a certain parameter is a reference parameter then the corresponding argument is said to be passed by a reference. And now this reference parameter enables us to not only exchange values but also compute polar coordinates given Cartesian coordinates and somehow convey the values back to the calling program. So basically the idea is that we are going to use 2 reference parameters and the call function will store the polar coordinates in the reference parameters. And these changes can be seen in the main program. And by the way this is not the only way of returning 2 values, we will see later that there are other ways as well but this is one of the ways. So here is the Cartesian to polar program. As you can see x and y are ordinary parameters, they are passed by value, r and theta on the other hand are reference parameters. So what does that mean? So when this call c to p happens the values 1 and y are copied into x and y but the values of r and theta are not copied. Now this function is going to execute and place some value in r and this r is really the same as this r because this is a reference parameter. So this value will actually get stored in the activation frame of the main program and in particular in the variable r in the activation frame of the main program. And similarly this theta because it is a reference parameter and because the fourth argument here is the theta of this main program, this theta is just a name for this theta and therefore whatever is calculated over here will get stored in the activation frame of the main program in the theta variable over here. So as a result what gets printed is going to be the r which was calculated over here and that theta which was calculated over here. So changing them in c to p changes the value of r theta in the main program and hence what gets calculated so square root of 1 and 1, 1 squared plus 1 squared so square root 2 will be printed for r and 8 and 2 of 1, 1 is 45 degrees so pi by 4 will get printed over here. So here is a quick exercise. So write a function which takes a length in inches and returns the length in yards, feet and inches. So this is the so-called British system which is not really in use but which is well which does get used once in a while and so you should note that 12 inches make a foot and 3 feet make a yard. So as an example if you have 100 inches then that is equivalent to 3 yards, 2 feet and 4 inches. So that is what you are supposed to print. So clearly you are expected to be returning 3 values and therefore you should have 4 parameters one in which you will pass the given length and the other 3 in which the function will put the number of yards, number of feet and number of inches. So what did we discuss in this segment? So we said that if you want to return more than one results we can do so by using reference parameter. If we use a reference parameter r in some function and pass as argument a variable a then any change that the function makes in r will be seen by the calling program as a change in a. In the next segment we are going to discuss pointers which will perform a similar function but pointers are quite useful in other places as well. So we will see that shortly. Let us take a break.