 Hello and welcome to week 5 of the NPTEL course, an introduction to programming through C++. Today's lecture sequence is about functions and the reading for this is from chapter 9 of the textbook. So the central question that functions address is how do we define new commands? So we really already have a whole bunch of commands for example square root of x, so square root is a command and if you pass it the argument x then you get the square root of x, forward is a command and given an argument d it causes the turtle to move d pixels. So you might ask are these the only commands that we can have or can we ourselves make new commands? For example we would like to make a command gcd of m,n which should evaluate the gcd the greatest common divisor of m and n or suppose we want the turtle to draw dashed lines as it moves rather than solid lines, continuous lines perhaps we would like to have a command dash d which would do this where d is the number of pixels that you want the turtle to move and in fact you can do this and the name the technical name for what we have been calling the command so far is a function. So in this lecture we are going to do the following things, we are going to give first with some examples of defining and using functions then we will talk about how to define a function in general, how a function executes, we will talk about what is called the contract view of functions. So how should you think about functions informally? Then we will talk about how the arguments can be given or what is called parameter passing and there are various ways of doing this, one is the so called parameter passing by reference which we will talk about and we can also pass parameters using pointers. So this is also a concept that we will introduce and finally we will talk about how functions can interact with graphic subjects such as rectangles, circles and of course turtles, the turtles we will already have discussed in the earlier part of this sequence. So let us begin with the problem, our problem is write a program that prints the GCD of say 36, 24 and 99, 47, here I have explicitly stated the numbers but in general you can say that I want to write a program which first finds the GCD of one set of numbers may be finds a GCD does something and then finds the GCD of some other set of numbers maybe does something else and find a GCD of third set of numbers and so on. So this is just a representative program representing the kinds of things you might want to do. So how would you do this? Well using what you already know you might make two copies of the code that we wrote earlier for finding the greatest common divisor. The first copy would be used to find the GCD of 36 and 24 and the second copy to find the GCD of 99 and 47. So here is what it might look like. So we have a main program in which we first define m to be 36, n to be 24 and then while m mod n is not equal to 0 we set r equal to m mod n, m equal to n, n equal to r and this is just the code that you saw earlier. So let me not explain that code but the result the GCD will be contained in the variable n at the end and so we can just print out the GCD. So that finishes the first part of what we were required to do. The second part is doing the same thing for m equals 99 and 47. So here we might start off by saying m equals 99, n equals 47, note that we are not re-declaring the variables. The variables have been declared earlier and we can just use those variables. Then we have the same code again and at the end again we print out whatever is contained in n. So this is a program which does what we wanted which is print the GCD of 36 and 24 first and then the GCD of 99 and 47. Now this contains code duplication. Code duplication is not really good. So if I am copying code maybe I will make mistakes in copying. So why increase the chance of making mistakes? Suppose I need to have GCD, the greatest common divisor, computed at 10 places in my program then it does not seem a good idea to make 10 copies of this code and this becomes worse if we need to make a change to the code that we wrote. So now I have to remember to make the change in 10 places. So all this is rather inelegant. Ideally you should not have to state anything more than once. And functions allow you to do exactly that. So I am going to show you the code which uses functions and it has two parts. The first part which we will see in the red is the function definition and then there is the main program. So here it is. The red part is the function definition and the green part is the main program. The main program calls or invokes the function. So the first call is GCD of a, me. Now I could have put 36 in place of a that would have been perfectly fine but just to show you that I can also have a variable I have put in int a equal to 36, b equal to 24 and called or invoked the function GCD with names of the variables rather than the values directly. And notice that I am invoking the function and I am expecting that the value that comes will sort of sit in the place where that call is, where GCD of a, is. Then I can directly print. In the next line I am invoking a calling GCD again and this time the arguments 99 and 47 I am passing directly, I am sending and writing them down directly. So in general the call is going to include the values whose GCD is to be calculated. The values are a, b in the first call and 99 and 47 in the second call. So the values supplied as a part of the call are called arguments to the call. The red part is the function definition. So first it contains a function name. Well GCD is the name before that there is this int so that is not the name the name is the GCD. And after that it contains int m, n and that says so GCD together with int m, int n says how the function is going to be called. So GCD needs to be supplied an integer argument which could be referred to as m, which will be referred to as m inside the GCD code itself inside this red code and it needs to be supplied another integer value. So that is what the top line is saying and the first int at the beginning of that code is saying that the value returned is going to be an integer value. So this int is saying that this GCD the call to GCD is going to return an integer value and this part is the so-called body of the function and this says what you need to execute in order to compute the GCD. Now I have given an example over here but I just now want to state what is going to happen in general or how would you write functions in general. So in general you would have a return type for the first word of the function definition is the return type. So indeed this int is the return type it tells you the type of the value that is going to be the result of the function execution. Then there is the name of the function over here and then the parameter names. So in the calling program these are called arguments and in the function these are called the parameters. So the parameters you can give a list of parameters but each list has two parts. First is the parameter type, parameter one type, parameter one name. Then parameter two type, parameter two name and so on as many parameters you want. So in this case we only give two parameters and then there is the body. So the function body has to be placed inside braces. So in our code this is the function body and the explanations I have already given you the return type is the type of the value returned by the function. For example int in our case and sometimes functions may not return anything at all and this is discussed later but in this case the return type will be written as void void, we will see an example soon enough. The name of the function could be any identifier name. So for example gcd as we have used over here and parameters are variables. So that will be used to hold the values of the arguments to the function. In this case these are the parameters m and n and these will hold the values of the argument. So we will see the exact execution process in a minute and the function body is the code that gets executed. So how does the function execute? So let us say you compile this and you load it and now you are executing the main program. So the main program starts execution then the main program executes, you execute this so a gets the value 36, b gets the value 24 and then the main program or the control the control in the main program reaches this statement and you see that there is a call over here. So at this point the main program is going to suspend. So this execution of the main program is going to stop over here. But the main program is not going to go away, it is going to wait. So suspend means that it is going to stop but it will wait. We can ask it to start again at some later time. Then so the main program has stopped at this point. It has said that look I need this expression to be evaluated. So now you make, now C++ makes preparations to run GCD and in some languages this is actually called a sub program. So it really is pretty much like another program but it is not the main program and so you may call it, you may think of it as a sub program. So some preparations are made. In particular an area is allocated in memory where the GCD will have its variables. So of course a program needs some area in which its variables are to be stored and main program also has it and main program will have an activation frame in which these variables will be stored. So similarly C++ will create an activation frame for GCD as well. So GCD's variables will somehow be stored over there as and when needed. Now immediately as soon as this activation frame is created, C++ also creates variables m and n. So these parameters actually become variables during the executions of GCD. Not only that the values of the arguments from this call are copied into m and n. So the value of this first argument is copied into m and the value of the second argument is copied into n and because the values are copied, so it is the value that is copied this is called passing arguments by value. So you will see there are other ways of passing. So right now we will just say that this is passing arguments by value. So a which has the value 36, so 36 will get copied in m over here and b which has the value 24 and therefore 24 will get copied in n. Now at this point execution of GCD will start. So what happens? So this is exactly like the execution of the main program. So these are the variables so far and now we execute this code. So you have seen how this code executes. So 36 and 24, we know that this is going to calculate the correct GCD. So n at the, so when all of this is executed, n will have the value 12 at the end of this. So all how to execute all of this is very clear to you. So when you come to this statement, this is a new statement. So let us explain what that return means. So n equal to 12 is calculated and the execution comes to an end essentially when we encounter a return statement and the return statement is going to somehow send back the value. So what value it sends is this, the word following return, the expression following return. So this value is going to be sent. But this value n has value 12 so somehow we are going to send back 12 to the calling program and where does this value go? So it goes and effectively sits instead of the call. So we are going to essentially have a 12 come in over here. So at this point, the purpose for which we started off running the GCD subprogram is done. Its code has been executed, it has returned its value. So the activation frame that we created for it has been, will be destroyed by C++. And in fact these variables m and n will also go away at this point. So this memory will be taken back by C++ and at this point we will resume the suspended execution of main program. Remember, we suspended the execution when we encountered this call. So now that that value has been evaluated, the value of GCD of a, b has been evaluated as 12, we resume the execution. So when we resume the execution what happens? Well, if there were 12 over here, we just want to print 12. So 12 would get printed, sorry 12 would get printed and after that this main program execution would continue and everything would start with GCD of 99 and 47. So the same thing would happen, again 99 and 47 would be copied to m and n and again so GCD, the first activation would be, frame would be created, gcd of m and n would be copied and so on. So this is how this main, this program is going to resume and it is going to print values or it is going to calculate values and send them back to the main program and the main program can do whatever it wants with the values. So maybe before I go to the remarks, let me stop here and let me just show you this program in action. So this is our program. So the only change I have made to the code is that inside GCD, I have put in a line saying calling GCD with arguments m and n. So what we are expecting over here is that when we execute the main program, GCD will execute but as it executes everything that we said a moment ago will happen but in addition it will also print this message. So the first time we should see a message 3624, the next time we should see a message 99, 47 and in between we should see the GCD is calculated. So let me just compile this code and let me execute it. So what has happened is that it indeed printed the message saying calling GCD with arguments 3624, 12 got printed because we were printing out the GCD and calling GCD with arguments 99, 47, 1 got printed because here the GCD is 1. So you could ask what if I print the value to be returned before I return it, yes you could do that. So you should try it out, take the program add that see out statement and you will see the value 12 printed twice and also the value 1 printed twice twice and maybe you could even print a message saying returning value so and so from GCD. So let me make some remarks about what we have seen. So we have a set of variables in the calling program. So here the calling program was the main program and this set of variables lives in the activation frame of main program and is completely different, is completely disjoint from the set of variables in the call function. So in this case the call function was GCD. Now both may contain the same name. So I had A and B in the main program and M and N in GCD but that is not needed. I could have named the parameters A and B if I liked. So that is not a problem. So when I execute the calling program, the code that is written there is expected to refer to the names in its body and in its parameter list. So if there was an M and N in both places and if there was the code for GCD contained M and N that M and N would refer to whatever M and N is defined inside the body of the GCD or inside the parameter list. So it had better be defined only there and of course the call program, the calling program, so it is the same rule for the calling program and the call program. So whatever variables appear in GCD are accessible in the code of GCD and whatever variables appear in the main program are accessible in the code of the main program. So there is no problem using the same name in both cases. So it is clear which, so if you refer to a name in your code, which declaration, which definition of that name you are referring to. One more point, when you pass the values of arguments, here we had written either the numbers 99, 47 or names of variables but that is not necessary. You might have done some calculation and then decided what value to pass. So you could put an expression instead of either the values directly or instead of putting the names of variables which contain the values. So if you put an expression, then that expression is first evaluated and then copied to the parameters of the call function. An important point is that in C++ there is a very general rule that if you are referring to a name, that name must be defined earlier. So in the main program we refer to the name GCD. So the GCD has to be defined earlier. If GCD is defined later, then that would not be correct. C++ compiler expects that GCD should be defined before it is actually used. So some more remarks, the body of a function can contain practically anything. Anything that say the main program can contain. So for example, it can create new variables internally if it wants to and the same rules apply. So if you create a new variable inside a block and the body of a GCD is a block in fact, so those variables will go away when the execution of that body ends. Inside the body of GCD or inside the body of any function, I can read values from the keyboard. I can use CN and also C out statements to produce output. So you already saw an example, we put a C out statement inside the GCD function. And just to clean this up completely, let me just observe right now and this will become more obvious a little bit later. The main program is actually also a function. So GCD is also a function, so essentially the same rules apply in both cases. Well, in the main program, we have not been writing return, but that is something, that is a concession which is given to the main program. So we could write return there and we will talk about that later as well. But in general for a function, you really should be writing return and we have already seen in case of GCD, we return the value that was meant to be the evaluated value of the call GCD whatever arguments being supplied. Then inside the body of a function, you can access the canvas and you can access the turtle created using turtle sim. In fact, you can make the call turtle sim also inside the body of the function, that is okay. So that will also create a window and that window can then be used even after you return to the main program. In fact, you can create other graphics objects also inside the body of a function. And we will discuss that a little bit later, you can create the objects, you can pass objects to a function, a graphic subject, but we will talk about that a little bit later. So here is a quick exercise, I am going to give you the solution. So you should try and do this yourself before proceeding further, so what is the exercise? Write a program to determine whether a number is even. So I would strongly request you to pause the video here and try and write this program. So assuming you have paused and assuming you have written the program, let us now see how that program could be written. So I am going to show you two ways. So the first way is okay, first of all we have to define the function. So what is the value that is going to be returned? So we are asking, we want to determine whether a number is even. So the answer expected over here is yes the number is even or no a number is even or true the number is even, false the number is not even. So indeed even we will write a function called even and it is expected to be returning a true or false value and therefore we have the return type as bool. So if you remember bool is a type whose acceptable values are true or false. Then this is the name of the function and it takes one argument. So this is the number or the parameter is n and this function is going to tell you whether this number is even or odd. So if it is even it should be returning true, if it is odd it should be returning false. Now how do we determine whether a number is even? So if you remember we have the percent operator which gets the remainder when one number is divided by another. So what we want to know over here is what is the remainder when n is divided by 2? Is it 0 or is it not 0? So if it is 0, if n mod 2 is equal to 0 then we want to return true because then the number is true otherwise we want to return false. So that is the function. Now this is a perfectly fine function but I just want to alert you to the fact that you can write this much more compact. So here is the compact form. So here what I have done is I have put in an expression over here. So instead of having an if statement I have put in an expression. So let us just check that. So if n mod 2 is equal to 0 then this expression will turn out to be true otherwise it will turn out to be false. So depending upon what this expression evaluates to a true or false will be returned. But when will this expression be true? It will be true only when n is even. So when n is even it will return a true and if this expression if n is odd this expression will be false and therefore a false will be returned. So both forms are acceptable but this is a more compact way of writing it and I just want to alert you that you can have an expression over here and in fact this is a Boolean expression. So Boolean expressions are also perfectly fine. So what have we discussed so far? So we discussed what is a function, we discussed how to define a function, how it executes and next we are going to take more examples and we are going to see how we are going to think of functions. So let us take a short break.