 In this video, I am going to introduce you with the functions using call by reference in C language. So learning outcome of this session is at the end of this session student will be able to write a simple program using function with call by reference method. Pre-requisite for this lecture are basics of pointers and functions in C. A function is a block of statements that perform a specific task. Hiring a function is something like hiring a person to do some specific job for you. If you need to perform same task more than once, it is advisable to create a function to perform that task and just call it every time you need to perform that task rather than writing the same set of statements every time. In C, main function calls either user defined function or built-in functions. Here we are focusing on user defined functions. Calling function has to pass some values to the called function. This is done by passing some parameters to the called function. The arguments that we use in calling function, usually the calling function is a main, maybe sometimes different. So the arguments that we use in a calling function are actual arguments and those in call function are formula arguments. Now there are two ways in which arguments or parameters can be passed. Alternatively words can be used, arguments or parameter has the same meaning. So there are two ways, one is a call by value, another is a call by reference. In call by value, values of the variables are passed by calling function to called function whereas in call by reference, address of variables are passed to called function. We will now learn the technicalities involved in passing arguments to called function. If a and b are the local variables in main that is actual parameters and x and y are formal parameters in function then in call by value method the value of a is copied inside x and value of b is copied inside y. Values of x and y are modified in function, these are the things in function. So x and y are the formal parameters which we use in a function. If values of x and y are modified in function, the values of a and b are untouched, they will not get changed in a calling function. As a function can only access its local variable, in case the value of a or b has to be changed then the programmer has to write a return statement through which one value either x or y can be returned. It can be returned back to main in which in one of the variables that is a or b. So in this way one of the variable may get changed but we cannot write more than one return statement in a program. Hence we cannot change more than one variable in a calling function using call by value method. If we want the value of actual arguments, actual arguments a and b, if we want the value of actual arguments should get changed in the called function then it is always appropriate to use call by reference method, let's see. In this method the addresses of actual arguments are copied into formal arguments of called function. With the help of these addresses called function can reach the actual arguments that is local variables of main and modify them which is otherwise not possible because local variables of one function are not accessible to other function whereas global variables are accessible to all functions in a program. Now here you can see a and b, a and b are the actual parameters. So here address of these variables are being passed and in x and in y we are collecting the addresses. As we said addresses are passed to the called function. Then what kind of variables need to be declared as a formal arguments, are they same as that of the actual arguments, think for a while for this question and give the answer. So whether the formal parameters or formal arguments are same as that of the actual arguments, their type or their kind of the variables. The answer is no, they are different. As we are passing addresses to collect these addresses we need pointer variables because we are passing addresses. So we need pointer variables of the same type. This was not the case with call by value. In call by value if actual parameter is an integer then formal parameter was also integer. Now let us understand the beauty of call by reference and the pointers with the help of program. The best example to understand this concept is a swapping the values of two variables. Now in this program you can see in main we have declared two variables a and b to whom we have assigned the values 10 and 20. Now here we are going to use a swap function. So before calling the swap function we will print the values of a and b. So before swapping values of variable a is equal to %d, b is equal to %d. Now the values 10 and 20 are going to get print. 20 means what? We have to simply interchange the values 20 will come here, 10 will go here. Now we are calling the function swap. So this particular statement is a function call statement. We are calling function swap. Now inside this we are passing the address of a and address of b as a parameter. So as we are passing the addresses we need pointer variables to collect these addresses. This is a function call statement. Now whenever a function is getting called the control goes to the function definition. Execution will start from the function that is to be that is being called. The next statement, printup statement is not going to get executed rather this particular function is going to get executed. Now address of a is passed. It is collected inside x which is an integer pointer variable address of b. It is collected inside an integer pointer variable y means 3050 is collected in x, 4000 is collected in y. Inside swap function we have used one temporary variable. Now this temporary variable we have used for keeping the value of one of the variable a or b so that we can interchange it. So first of all we will bring value of a inside temp then b's value can be passed to a and then temp's value can be copied over here in b. So this is done using these three statements temp is equal to star x. Now inside means temp we are assigning with what value at x star x means value at x. Now let's see value at x, x means 3050, go to 3050 address value at this is 10. So assign 10 to 10 then star x is equal to star y value at y value at y means what value at 4000 go to the 4000 address value is 20. You just assign this 20 to star x star x means what value at 3050 means assign 20 at the address 3050. Now star y is equal to 10 similarly copy this temporary into star y means value at 4000 go to 4000 address and store this value 10. In this way 20 has moved to a 10 has moved to b. Now here this is a function declaration statement this is function call statement and this is what is a function definition. So thus this program manages to exchange the values of a and b using their addresses stored in x and y. Using call by reference intelligently we can make a function return more than one value at a time. So even though we have not used two written statements but still we have managed to return two values with call by reference method. Had it been a call by value method swapping would have done in formal parameters x and y only whereas the actual parameters a and b would have remained unchanged. You can see this is the output before swapping the values were 10 and 20 after swapping the values have got 20 and 10. So if you want that values of actual argument should not get changed use call by value method. If you want that the values of actual argument should get changed use call by reference method. Similarly if you want your function to return more than one values then this can be achieved indirectly by using call by reference method. Even a mixed call can be made for example if you want a function to calculate an area of a circle radius can be passed by value and area variable can be passed by reference because we do not want to change the radius in main whatever radius is there we do not want to change it in the function but we want the calculated value of area in an area variable in main. So here the mixed call can be made these are references thank you.