 So, in the last segment we discussed what is a function, how to define it and how it executes. Now, we are going to start by taking more examples. So, another exercise, so let us write a function to compute the LCM or the least common multiple. So, here we are going to write this code but we are going to use the identity that the LCM of two numbers M and N is their product divided by the greatest common divisor. So, now we already know how to calculate the greatest common divisor. So, you might say that look then why do not we use this and calculate LCM as well. So, here is the code we are going to write in LCM of M and N it is going to take two arguments it will have two parameters M and N and we are going to just return that expression. So, return just that that is it and of course this assumes that you have GCD already defined and where should that be defined it should be defined before it is used. So, it should, so the definition should come above LCM, so your code should look something like this. So, first in your file you should have the GCD program then you should have the LCM program and I have not given the full GCD program it is a little bit long but it will return N eventually and so N of course with the value of N will have changed inside this and here I am going to it is going to return the LCM and then I am going to have the main program. So, the main program is going to make a call on LCM, LCM will make a call on GCD which is given over here. So, how does this execute? So, let us see that, so the execution begins with the main program so the main program starts executing, so it encounters this statement and it sees oh there is an LCM to be computed. So, since there is an LCM to be computed the main program suspense and an activation frame is created for LCM, then 50 and 75 are copied to M and N, so 50 and 75 are copied to these M and N and LCM starts execution. Now, the call to GCD as this executes the call to GCD is encountered, this execution of LCM tries to construct this value and it encounters a call to GCD. So, because of that LCM is suspended, so now note that we have two things suspended, we have the main program suspended and we have the LCM suspended and at this point the activation frame for GCD is created because this value has to be calculated, so 50 and 75 these values are copied to these M and N. So, in the activation frame of GCD, so execution of GCD starts, GCD computes the correct result, the GCD of 50 and 75 which is 25, so GCD computes this result and the result is copied to the activation frame of where this call came from, so this call came over here, so 25 gets copied over here, so now GCD execution is complete and so the activation frame of GCD goes away and LCM execution resumes, so LCM continues execution, so sorry this, so M times N divided by 25 is calculated, so 50 times 75 divided by 25 which is 150 is calculated, but this is the return statement, so this value gets copied in place of this expression, so the value is returned to the main program and the activation frame of LCM is now destroyed, so remember what happened, so main program had its activation frame and activation frame was created for LCM and activation frame was created for GCD, the activation frame of GCD was destroyed, the activation frame of LCM was destroyed and the value 150 was returned from here and it sits in its place, so at this point the main program resumes execution and prints 150 and then it exits, so one more quick function, so we want to draw dashes while moving, so we have our turtle and normally if we just say forward it draws a line, so now we are going to draw dashes, so here is a function, we are going to have the name is going to be dash and we are going to say how many pixels we want to move, so we want to move forward d pixels and we want to say over here how many dashes should there be, so this distance is going to be divided into these many dashes, so how does this work, so this first of all this function let me observe is not going to return a value, it is going to cause some drawing to happen, so because it is not returning a value we are going to state its return type as being void, if you wanted to return a value in addition to drawing something we could have put that type over here, but as it is we do not, we are not interested in returning a value, so we want to draw 10 dashes or whatever n dashes, so we are going to have a repeat statement, so repeat n times and we are going to go forward, we are going to go forward d by 2n, so a distance d by 2n we are going to go forward and during this we are going to assume at the beginning that the pen is down, so a line will be drawn. Now at this point we will raise the pen, so at this point again we will start going forward, so we will have forward d by 2n again, so this time the turtle will move the same distance but with the pen up, so no line will be drawn, then we will put the pen down again and that would be the end of the loop, so this whole thing, this whole thing would be executed n times and so the turtle will cover d over 2 distance twice in each iteration, so d by n distance in each iteration, so overall it will indeed cover d distance and at the end it will return, so here note that we have not said return something, so here we are not expecting to return anything and therefore we just say return followed by a sin equal so that is it, so the main program could look something like this, turtle sin repeat 4 dash 105 and write 90, so this should be drawing a square, we have put in a get click just to see that that square has been drawn. So let me suspend the program over here and let us see this dashes program in execution, so here I have the dash program, so let us compile it and run it, so see the turtle drew a dash rectangle, we can finish, we can stop the execution by clicking and that is it, so at this point we have seen several examples of functions and now I want to say something about how you should think about functions, so a function you should think of as a piece of code which takes the responsibility of getting something done, the specification is a description of what the function is supposed to do, so typically the specification looks something like this, if the argument satisfy certain properties then a certain value will be returned or a certain action will happen, that is what the specification is going to look like. The certain properties are sometimes called preconditions, so for example for GCD you might say that if positive integers are given as arguments then their GCD will be returned, so this is a specification for GCD, if precondition is not satisfied, so if you for example give negative values then what happens, well nothing is promised, the program may run forever, the program may return some nonsense of course but nothing is guaranteed to you, the writer of the functions, the writer of the function GCD is not making any claim about what will happen, you could say that if the values supplied are incorrect then the function should say so but that is not really always required, so the function writer has to say that these are my preconditions, so it is your responsibility to check if the arguments satisfy the preconditions, if not I do not know what is going to happen, but you could have other function definitions in which there are no preconditions because if the wrong arguments are given the function may take some action and return some value saying that oh you gave me wrong arguments. Before you write a function you should write its specification as a comment in the code, so this is just so that you are sure yourself that you know what it is that you want to do. So there is a certain kind of humility that you have to have when you write programs and I think most programmers have that humility because they might write programs and they might claim that oh this program is going to do this but actually when they execute they find that no the program does something else and then they have to spend lots of time figuring out why the program made a mistake. So because of this humility it is a good idea that you start off the whole process saying let me be clear to myself what is it that my program is going to is supposed to accomplish and therefore let me first write down the specification in as clear words as possible. Now all this is really taking you towards some kind of a contract view, what does that mean? The writer of the program is promising something. So a function is a contract between the programmer who wrote the function and the program and the programmers who use the function. Sometimes both of these parties could be the one and the same party but they need not be. So if they are not then there is the contract view sort of makes more sense. The programmer who uses the function says that look I trust the function writer to make sure that the execution satisfies the specification. So when I make the call I am not going to worry about how the function is actually going to execute that is not my job. So the whole programming process is sort of to do things like this. So once I write a function subsequently when I use it myself I am not going to worry about how that function executes because when I wrote the function when I tested it I have satisfied myself that it runs correctly and from that point onwards I am not going to worry about how it works internally. And if it is some other parties function the some other party wrote that function then of course I am not going to care how that other party wrote the function. In fact I am not going to care even if I wrote it myself because when I wrote it I would have tested it and I have sort of made sure that it works correctly. So now I do not want to be bothered with it. Programmer who wrote the function does not care which program uses it. So I write a function then I am writing the function I am trying to make sure that it is going to run correctly no matter who uses it no matter what arguments are supplied to it. So it is sort of like giving cloth to a tailor. So when I give the cloth the tailor promises to give me a good shirt if the cloth is good and the wearer does not care how the shirt was stitched and the tailor does not care who wears it. So it is a contract between them that I give you a cloth you give me t-shirt you give me shirt. Similarly for a function it is a contract between the function writer and the function user. So the contract is made so that the responsibilities are clearly delineated and each party does not reduce it is meant to reduce the worry for each party. So the user does not worry about how exactly the function was written how it executes and the party that wrote the function does not worry about where is it going to get used because while writing the party has made sure that it is going to be correct in all cases. When the function finishes something is promised and in particular something might be promised about the state of the computer. So for example after the dash program finishes its execution if you go back and look at it it was going to leave the pen down and it was also expecting the pen to be down. But you may change this you may say look I would like you to assume that the pen is up so maybe you should yourself make sure that the first command is pen down and I want you to leave the pen up so maybe you should do that. So whatever it is whatever the expectations might be it is a good idea to spell them out clearly so that the user of the function is not surprised and the user of the function does not expect more than what he or she is going to get. So in this case here is a small exercise modify that code so that these post conditions will be satisfied and of course the post conditions must also be mentioned in the specification and this cannot be stressed too much writing clear specifications is very important for the writer of the function as well as for the user of the function. So what did we discuss in this segment? So we started off at the very beginning by saying that if you are going to execute the same code several times possibly with different values then you define a function that executes the code. You call the function with the appropriate values. Functions can have their own variables which are created in the activation frame or activation area of the function and the names can be the same in the main program and in the functions. We also said that the function is like an independent program except that it gets some values the arguments from the calling program and returns results to the calling program. We are going to talk about reference parameters and we will take a short break.