 In the last segment, we talked about pointers. In the next segment, we will conclude this general discussion of functions, the basics of functions. But before that, I want to say something about functions and graphics objects. So here I have a program in which I am using graphics and I am using canvas on which I have a rectangle created and then I am calling that rectangle using a function rev 360 using this rectangle r, passing this rectangle r as an argument. So I can do this and I want to explain how all this executes. So you can pass graphics objects to functions as it has happened over here. Now the parameter must have the same type, that is the same, it must be the same shape. So indeed this r is a rectangle and in fact this r is also a rectangle. But not only is it a rectangle but it is a reference parameter. So that means this r will really be the same as this graphical object. So whenever I am referring to r in this body, it will refer to the same graphical object that I had created in my main program. If I had not put this and over here, then the usual rules apply. What are the usual rules? Whatever argument is this argument, its value is copied into this. So at this point I want to tell you that when I talk about a graphics object, so say something like this r over here, this r is really doing some kind of double duty. So nominally it is actually a variable also. So it is a variable created in the activation frame of main program and it is actually the variable which contains all the information about that rectangle. I am not going to tell you what information it contains but just assume that it contains whatever information there is. So whatever information is needed to say draw it on the screen. So in some sense the drawing on the screen is kind of a byproduct. So we are really creating a variable. When we write this, we are creating something on the screen but we are also creating a variable. So that variable gets passed over here but it is passed by reference. So really this r refers to the same graphical object on our screen. If I passed it by value or if I did not have that and over here, what would happen is that a copy would get passed. So not only would the variable be copied but the way this graphic system has been designed that graphical object would also be copied. When that copy is made initially the object would be at the same position as the object from which the copy has been made. But you would get a new copy but that is not what is happening over here right now. We are getting, we are really passing the object by reference and therefore this r is really the same as this rectangle over here. So what is this going to do? It is going to repeat but it is going to rotate by 10 degrees. So and then it is going to imprint. So it will rotate and at every point it will imprint. So what will happen is you will see a rectangle first but it is going to be, it will rotate and as it rotates it will imprint. So all these rotated positions imprinted on the screen and so on. But at the end the rotation, the total rotation is 360 degrees because 36 times and every time we are rotating by 10 degrees. So at the end the original position will be attained. So it starts off in some position and at the end the same it will come back to the same position. But as it does that it will be imprinting, it will be imprinting on the screen. Now the point is that the imprinting on the screen is going to survive. So even after, even after it returns the imprinting will stay there. So we said this that if you pass by a value, a copy of the variable is made and a copy of the graphics object is also made and I should say that as well that if you made a copy of the variable and a copy of the graphics object, the variable as well as the graphics object would get destroyed when you return. Whereas in this case there is no destruction. So that r of the main program is going to stay around and that is the r that is going to get used up over here. If you imprint that imprint happens on the screen and it will survive after the call finishes. And by the way I mean this copying business is allowed otherwise as well so I might as well state that here. So for example here I have created a rectangle by defining r but I create another rectangle s and I say that I want s to be exactly equal to r so I assign it. So I can do that as well. So really when I make this copy it is really sort of like an assignment statement. Anyway the point of this is that when I have graphics objects I can pass those graphics objects and I can do things with them if I want. I would like you to think about a few exercises. So here is an exercise which might be quite interesting, might be quite useful if you are going to design some bigger programs. So you have to write a function which takes a rectangle and coordinates x, y as input and decides whether the point x, y lies inside the rectangle. So this might be used to say that I am going to draw a rectangle on the screen and then I am going to do a get click and I would like to know whether the user clicked inside that rectangle. So this function would come in handy. Now you would have to pass that rectangle to our function and inside the function you want to get information about that rectangle and compare that with the x, y arguments that are also being passed. So if you do r dot get x then you will get the x coordinate of the center of the rectangle. If you do r dot get y you get the y coordinate of the center of the rectangle. If you do r dot get width you would get the width of the rectangle. So now you could see that by getting the width and the height and the x, y coordinates you can check whether the point x, y the x, y coordinates that have been given to you form a point which lies inside the rectangle. So this way you can see whether the user is clicking on a rectangle and if the user is clicking on a rectangle maybe you could write a program which changes the color of the rectangle, color of the rectangle if the user clicks on it. So this could be a fun program. So as I said I am going to conclude in this segment but the conclusion is for the entire sequence of this lecture. So this basic lecture about functions. So what did we say? We said that if you find that you are performing the same operation at several places in your program then consider putting it into a function and do not duplicate code but make a call. You can think of a function as a contract that we talked about earlier but you can also think of it as a packaged software component. So just as you buy packaged hardware components. So anything that you buy practically is a packaged hardware component say you buy a television you do not know what is going on inside it or if you buy a refrigerator you do not know what is going on inside it. You are told you are given some specifications and that exactly is how you should view functions. So it is packaged and its specifications are told to you but you do not really need to look inside to see how that whole thing actually works. You can just believe the specifications you are expected to believe the specifications and if that does not work then you can of course sue the manufacturer or warranty period you can get things replaced but that is what really all this is. So the function the packaged component idea is sort of like the contract idea that we talked about earlier. Then we said that arguments can be passed by value and here if the corresponding parameter is modified in the function no direct effect happens in the calling program. Arguments can be passed by reference in which case if the corresponding parameter is modified in the function a variable in the calling program will change. If the argument is a pointer to a variable in the calling function then the code in the calling function can access the variable by dereferencing the pointer. Now there are lots of things that you can do with functions. So for example you can write a function that draws an n-sided regular polygon such that each side has side length x, s and returns the perimeter of n times s as the result or you can write a function that returns the cube root of a number using Newton's method. So really all the algorithms all the methods that we discussed in an earlier week you can package it as functions and then you can just make calls so that you do not have to copy code any longer. And there are other similar exercises given at the end of chapter 9 and I definitely suggest that you try them. So that concludes this basic lecture on functions and we will stop here. Thank you.