 Hello and welcome back. In this lecture we are going to see the use of pointers in function calls in C++ programs. Here is a quick recap of some of the relevant topics we have already studied. We have looked at basic programming constructs in C++. We have seen the pointer data type and the address of operator and the content of operator in C++. However, we have basically used the address of and content of operators within the same function in all the examples we have seen so far. In this lecture, we are going to actually use these address of and content of operators across functions. So, we are going to see the usage of pointers across functions. We will see how pointers can be passed as parameters to functions, how this compares with call by reference and we will also see what happens when functions return pointers. We are going to talk about memory addresses and pointers a lot in this lecture. So, let us quickly recap what these terms mean. Main memory is basically a sequence of storage locations. Each location in main memory contains a value which is exactly 1 byte or 8 bits. This is also called the content of that location. Each location in main memory also has a unique address. A pointer is basically an address of a location that is allocated in main memory to store a value and pointer valued variables basically store such addresses of memory locations. We are going to talk about function calls. So, let us quickly recap what we know about function calls. We can pass parameters to functions in one of two ways call by value and call by reference. We know that activation records are pushed and popped from the call stack which is stored in the stack segment and these activation records and the call stack are basically the mechanism by which we can manage local variables passing of parameters and also flow of control when a function calls and when a function returns. We have also seen that all local variables of all functions in a program are allocated space in the activation record of the function. The activation record itself is pushed into the call stack and the call stack resides in the stack segment. So, all local variables of all functions will basically reside in the stack segment. So, now that we have briefly recapped pointers and functions. Let us ask, can we pass pointers as function parameters? Well, why not? C++ does not put any restriction on what type of values you are passing as pointers. So, we can indeed pass pointer values as function parameters, but then when we are passing values as function parameters, should it be call by value or should it be call by reference? For our purposes in this lecture, we mostly going to look at call by value. However, it is important to note that C++ does allow passing references to pointers as well. And in fact, when you pass a parameter by reference and when that parameter is a pointer value variable like n star or car star, the C++ compiler will treat it in pretty much the same way as it does when you pass a parameter by reference and that parameter is a variable of some other basic data type like n or car. So, let us look at an example program where we are going to pass pointers as function parameters. So, here is the main program. It has two integer variables. I ask for their values and read them in and then I call this function swap by pointer. Now, notice what am I passing as parameters to this function? The addresses of the variables m and n. So, I am basically passing pointers as parameters to the function swap by pointer. So, the declaration of the function swap by pointer, I have to say that its formal parameters are basically integer pointers. When I define this function, when I specify how it works, I also have to declare its two formal parameters as integer pointers. Now, let us see what happens when this statement is executed in this function. I am basically reading in the values of m and n. I am inside the main function. So, in the stack segment, I will have an activation record of the main function when the main function is called by the operating system, that this activation record of the main function is created. Now, this activation record will contain storage for all the local variables of main. So, it will have storage for m and n, m and n are integers. Each of these need 32 bits or four bytes. And here, I have shown these four bytes as four addressable memory locations in the stack segment. More specifically, the activation record of main. Of course, the activation record of main will contain a whole bunch of other bookkeeping information also. Let us not worry about those for the current discussion. So, if I provide 5 and 6 as the values of m and n, then 5 and 6 will basically get stored in the memory locations allocated for m and n in the activation record of main. Now, when I call this function, I have to create a new activation record. So, note that in that activation record, the values of the parameters that I am going to pass are actually addresses of integer variables. So, the parameters are actually addresses and I am going to do a call by value with these addresses. So, essentially, I am passing two addresses as parameters and I am going to do call by value when I call this function. So, these two addresses will get copied to the space allocated for the formal parameters of this function in the activation record that will get created for this function. So, here is the activation record of swap by pointer. Here is the space allocated in it for its two formal parameters and the addresses of the variables m and n in the activation record of main which is basically hex 740 and hex 780. Remember, when we talk about the address of a variable, we really mean the address of the first byte of the contiguous sequence of bytes allocated for that variable. So, address of m is hex 740, address of m is hex 780. So, these two values will now get copied. So, this is call by value. So, the values of the addresses which is hex 740 and hex 780 will get copied into the space allocated for the two formal parameters of swap by pointer. So, here I will have the values hex 740 and hex 780. What is this temp? You will soon see that swap by pointer also has a local integer variable called temp and this is basically space allocated for that in the activation record of swap by pointer. Once again, this activation record will have other bookkeeping information. We will not worry about that for the time being. Now, within the function swap by pointer, here is our local variable temp for which we have already allocated space and here is the first executable step statement. Temp is assigned star pointer x. Now, note that pointer x actually has a value which is the address of a variable in the activation record of main. So, when I ask it to evaluate star pointer x, I am basically asking it to get the value of a variable in the activation record of main. So, this is interesting because I am accessing contents of memory location in the activation record of main from swap by pointer. So, what is that value? It is hex 5. So, that will be copied into the memory space allocated for temp. So, that is what happens here. And then when I go to the next statement, what am I doing? I am saying that look at the value of pointer y which is hex 780. Go to the memory location with that address star pointer y is saying content of the memory location whose address is given by the value of pointer y. So, content of the memory location whose address is hex 780. Once again, this is actually in the activation record of main. So, I have to read that value from there which is I have to read hex 6 from there and I have to assign it to the memory location whose address is given by the value of pointer x. So, pointer x gives me the value hex 740. I have to treat this as an address. Go to the memory location whose address is hex 740 and update that with the value 6 that I just read. So, that is what happens here. So, I have basically updated m in the activation record of main with the value of n in the activation record of main, but all of the subdivision happened from within the called function swap by pointer. And how did I achieve this magic? By basically passing pointers to variables in the activation record of main as parameters to the function swap by pointer. And then by dereferencing those pointers, I get access to the variables in the activation record of main. What happens when I execute this statement? I am copying the value of temp which is 5 to the memory location whose address is given by the value of pointer y. So, the value of pointer y is hex 780. This is the memory location reserved for the variable n. So, basically n will get updated with the value 5. So, note that what actually has happened is the contents of m and n have gotten swapped in the activation record of main, but all of this has happened inside the function swap pointer. This has been affected by the function swap by pointer. So, now when I return from swap by pointer, what happens is the activation record of swap by pointer is gone. However, the values of m and n have already been swapped. So, now when I try to print the values of m and n, I will actually print the swapped values. So, the model of the story here is that by passing pointers as function parameters, the callee in this case the function swap by pointer actually got access to the local variables of the caller in this case main. So, this is really a way to share variables between caller and callee and we have seen that we can also pass parameters by reference to accomplish this. And it is very interesting that when we pass parameters by reference in a C++ program after compilation, it is actually converted to passing pointers as parameters. That is how the called function or the callee function actually gets access to variables in the caller function or calling function. It all happens through pointers, but their pointers act behind the scenes. We only say that we are passing a parameter by reference. Of course, when you pass parameter by reference, some more bookkeeping is done. And as we will shortly see basically passing parameters by reference is like a bit of a tidier way of passing parameters by pointers where the passing parameter by pointers actually happens behind the scenes. So, to illustrate this I have written a program here which also tries to swap the values of two variables. So, just like before we have two integer variables and we read in their values and then I call swap by pointer as before with the addresses of m and n and we have just seen that this is actually going to swap the values of m and n in the activation record of main. But after that I also have this function which is swap by ref mn and note that I am calling these two parameters by reference. So, in the declaration of swap by ref I have to say that these two parameters are called by reference in the definition of this function I have to say these two parameters are called by reference. So, when I call a function with pointer parameters I have actually got to figure out what the addresses what the values of those pointers are before I call the function and then the formal parameters must be a pointer type and within the function I have to use all of these stars to really do the dereferencing of these pointers. Whereas, when I pass parameters by reference I actually call them just using the normal variables and it is only in the declaration of the formal parameters that we say that these parameters are really parameters that are being passed by reference in the body of the function also I do not need to use any star or any other operator I can just use these variables x and y as if they were the local variables of this function. So, what you see is that we can think of swap by pointer as really the back end way by which the compiler implements swap by ref because in swap by ref this function needs to have access to the variables of main, but the only way to give access to the variables of main is to actually pass pointers to the variables of main. So, that we know exactly which memory locations we are talking about in the activation record of main. So, swap by pointer can be thought of as how the compiler implements swap by ref, but of course swap by ref is much cleaner to use we do not have all of those stars over here we can just treat the parameters as the local variables of this function. Now, can a function return a pointer most certainly, but when you return a pointer from a function you need to be careful that the pointer is actually pointing to a legitimate address in memory specifically if the pointer is pointing to a location in the activation record of the function that just returned that activation record is going to be freed when the function returns. And so, when you try to dereference that address you will not be able to see a valid memory address and it will cause your program to crash. So, let us illustrate this by the simple program. In this program I have an integer variable B and I have an integer pointer variable A. So, I read in the value of B and then I pass the address of B to this function myfunk and it returns an integer pointer which I store in A and then I try to dereference this integer pointer. Now, what does myfunk do? It has a local variable A it does some calculation on that local does some calculation and updates the value of that local variable and then it returns an address to this local variable. So, note that this is a local variable in the activation record of myfunk. So, basically this is an address of a local variable in the activation record of myfunk. So, when I return back to the main function the activation record of myfunk is gone it does not exist. So, the address that is returned back here by myfunk which was the address of the local variable A in myfunk is actually a bad address when I try to dereference it my program will crash. On the other hand here is an alternative program where I actually calculate the value of A and assign that value of A to the memory location whose address is given by the value of pointer B and what was pointed B? Point of B was actually the address of a location of address of a variable in the main function. So, when I return pointed B I am actually returning the address of a variable in the main function. So, although A is a local variable in my function, but the address of the variable I am returning is actually in the activation record of main. So, therefore, I get a legitimate dereferencing. So, in summary in this lecture what we saw is that pointers or addresses can be passed as parameters to functions and this pretty much achieves what we achieved using call by reference parameter passing and this is in fact what happens behind the scenes when you do call by reference. But we also saw that there are some caveats to be aware of when we are returning pointers from functions. Thank you.