 Good morning. This week, we are going to learn two very important constructs in C plus plus. We have already seen that from a simple sequence of statements, which when executed to desired computations, we first discussed the availability of conditional control structure, which permits us to take decisions and execute either of the two statement sequences. We then learn how to set up an iterative block, so that by writing few instructions, we can actually make computer execute hundreds of thousands or even millions times the same instructions, resulting in useful work done without having to write hundreds of thousands of lines of our program. We have two more important facilities, which will complete the discussion on basic programming principles. The first is the ability to write part of our program as separate blocks, which can be invoked or called whenever you require from within your main program. This notion is called sub-programs. In C plus plus, these are called functions. Functions are standalone code, which solve a particular part of the problem and which can be invoked as many times as you require from within your main program. We shall be looking at the facility of defining and using functions in C plus plus today. Next is our inability to handle a very large number of individual values, permitting access to any one of them during our program. Recall, we have written programs to add a large number of numbers or to find out factorial n. So, we could establish an iteration to do these things. However, if we have let us say thousand numbers and you want to read all of them and retain all of them and then access a 531st number any time in your program, we do not have that ability now unless we write thousand different names such as A, A, A, B, etc. That is provided by a facility called arrays, which is what we will be discussing on this Friday. So, before we look at the functions today, I would like to begin discussions by solving one more interesting problem, which requires an iterative solution, but it is a good computational problem coming out of the basic maths. We will use this to elaborate how iterations could help to solve problems numerically. However, we begin with two very special provisions, one of which we had actually seen in the switch statement when we discussed. It is called a break statement. So, this is your block where you have the typical while condition here. If it is true, you enter the block and then repeat it indefinitely till this condition remains true and then get out. You all agree and familiar with this while statement, while condition execute the block repeated. Inside these statements, if anywhere a special statement called break is encountered, you may have several other statements after this. The break is a very destructive statement. It simply takes the control out of the block straight to the exit path. So, it sort of jumps out. What it means is all the remaining statements will be ignored completely. In fact, we roughly did the same thing when we use break for different cases in the switch statement, but there was no iteration there. It was only a conditional execution. This takes the control out. Very obviously, if a break statement is written as I have shown, the control will always come out at that point. So, the break statement would necessarily be used in conjunction with some conditional check. If some drastic condition get out, then we can use the word break. So, this abandons the iterative block completed. On the other hand, if I have a similar block with the same condition as I shown and in this block, somewhere I encounter a statement which says continue. This continue statement also skips the remaining statement. So, both break and continue will skip the remaining statement. But in case of a break, we abandoned the block completely and went out to do things with the next statement. With continue statement, you only skip these statements and come out here to carry on with the next statement. So, there is a very fundamental difference between the two. Continue statement skips the remaining statements, but continues with the next iteration. Observe that this statement can be used. These continue and break statements can be used in any iteration. Whether it is established by file something or whether it is established by do something file condition or whether it is established by file. In case of file, you remember that there will be an increment on this part. There will be some increment condition here in case of file. I will call it file increment. So, this increment will be executed. When you come out to continue statement, it is not that it will blindly go to repeat the. So, it will follow exactly the same specification of iteration. Otherwise, it will only skip this block of statements. Once again, it is to be emphasized that break, like break, continue also cannot be used as a standalone statement. Because if you do, then every time you come there, you will skip the remaining statement. There is no point in writing subsequence. So, both of them are used in conjunction with some decision making. Like if some drastic condition break get out. If some condition, which says I do not need to execute the remaining things continue. I can implement both of these using appropriate changes in the condition for while iteration and using appropriate if conditional within the block, but this makes it convenient. That is all. So, this is the semantics of the break and continue statement. When used inside a control structure, break causes an immediate exit from the loop. Whereas, when executed inside a loop, the continue statement will skip any remaining statements and goes for the next. And the point you should notice should always be used as a part of, should always be used as a part of some conditional execution. Otherwise, they do not make sense. With this, now we proceed to discuss the problem that I promised. This is a computational problem. As an example of finding out real root of an equation, we consider the equation A x square plus B x plus c equal to 0. Everybody is familiar with this quadratic? There could be, there will be two roots, but both the roots could either be real or complex. What is the condition when they are real? B square minus a 4 a c should be positive. This everybody knows that. In fact, this particular quadratic equation form has a analytical solution. You can actually calculate if it has real roots R 1 equal to this, R 2 equal to this analogy. Can you do that with a cubic equation? Perhaps. Can you do that with a equation which is a polynomial of degree 4? What about degree 5? What about degree 6? What about degree 7? What about degree 28? And such large equations are not uncommon in scientific and engineering computing. Such roots therefore cannot be found analytically and therefore, numerical methods have been evolved to find approximations to these roots. Please note that in mathematics, we seek exact solution, but in most engineering and scientific problems, a close approximation is quite often good enough. And an approximate solution, if it could be found through some iterative technique of improving the approximation at every stage, it would be a good procedure. I have drawn it here because I was not sure I will get the stylus, but since I have it, I can draw things here. So, this is some function f of x. You will agree that one of the real roots of this function if it exists will be diagrammatically the crossover point at which x axis is intercepted by the function, because at that point whatever is the x, f of x is 0 which is the root. How to find this? So, what we do is we make an initial guess. You need not draw the function again and again, but let us say I do not know where this is by the way. So, on this x axis somewhere I will make a guess. I call this one and I call this as x guess. This is a starting point. X guess could be 58, 22.5 minus 7. We do not know that. Our ambition is that whatever is the starting point, we should now use the properties of the function to calculate another guess, a modified guess which is closer to the root. In this particular diagram, since root is shown here, our ambition would be to find some procedure by which we can move in this direction and let us say we can get a new point here which has an x coordinate x nu. We still not clear what property of the function will use, but this is our ambition. From x guess, we want to move to x nu. You can see that x nu is a better approximation to the root and you can also sense that now I can set up an iteration by simply treating x nu as a new value of x guess and repeating whatever procedure I did earlier so that I go further and further and further till I meet the root. So, this is what I said an iteration can now be carried out. Take x nu as the initial guess. How do you do that? Simply set x guess is equal to x nu and repeat the steps to find the next iteration. How to find x nu? There are many methods. One popular method is called Newton Raphson method. Suppose this is my original value of x guess, then what I do is at this point wherever the function is, say here I will draw a tangent to that graph. The tangent will look like this for bad drawing. So, this is the tangent to the graph here and the tangent will intercept x axis at some point. I will treat this as my x nu. Now, how do you calculate the distance between x guess and x nu? That is how we will calculate the value of x nu. To find this distance we use the property that tangent is always given by the derivative of the function. So, if the function is f of x, this is f of x, then f dash x at any point will give me what? The derivative gives what? Tangent of an angle subscribed by this tangent line to x axis. That is the property of the derivative of the function. Now, once I know this is tangent and I know what is tan theta. Tan theta is equal to this height h divided by this length l. Now, h by l is equal to tan theta, which I can calculate at x guess. How do I calculate h? Very simple, the value of the function at x guess. So, effectively knowing the value of the function at the guess point and knowing the value of its derivative at x guess point, I know I can find out the difference between x q and x guess. This is the crux of approximating a root in steps. In the first step I move to closer towards the root. In the next step, we move further close etcetera. So, this is what I have tried to show here. There is a point p on the x axis, which I call the initial guess. I want to find some point q, which is closer to the root. How to find q? First of all, I note that the length p r is nothing but the function value at x guess, which is the starting point. Then I draw the tangent to curve at that point r. q will then be intersection of the tangent with x axis. This is what we just saw. Further, we note that the tangent is determined by the derivative. So, f dash of x guess will be equal to p r upon p q. We need to find out p q this distance. If we find out this distance, then x guess minus that distance is my new coordinate x for the root. So, p q is given by p r upon f dash x guess. p r itself is f of x guess. So, f of x guess upon f dash of x guess. Knowing that, I can find out x nu, which is simply equal to x guess minus p q. Please note that before writing any program, we will have to do this kind of analysis in most of the real life problem. I repeat this because most of you are used to solving problems, which are given as detailed specification. Do this, calculate the value of this formula etcetera. In real life problems will come in this form. So, Newton and Raphson applied their mind to do these computations. Of course, it is not easy to do these computations by hand for too long, too many times. But using a computer, we can make these iterations as many times to be executed as we desire. But first, what is the iteration block? The iteration is that whatever was the original value, I move to the new value. So, whatever is the x nu now, I will treat that as x guess and again repeat the process. So, I will go here, I will draw a tangent, I will come to this point, which will be q. I will find out p q and keep moving this. So, the crux is given x guess, find x nu and now I will treat x guess equal to x nu now and repeat the process and keep on doing this till you are satisfied. We apply this logic to find out square root of a given number, which is a easier. Have you calculated square root any time? You know how to calculate square root arithmetically. What is square root of 64 plus or minus 8? In this particular case, we are talking only of positive roots, but we could also get a negative root does not matter. One of them, if we get square root will be same, the sign could be plus or minus. What is the square root of 1792486.7784? How long will it take? No calculators. It will take a long time. This was the problem, which afflicted Newton and his colleagues, when there were no calculators. But the methods that they derived are still useful. We propose to use Newton Raphson technique to find the square root of a number. By recognizing that, I could model this problem of finding square root of a number by saying, let that number be x square. Then, if I solve the, if I rewrite this equation k is equal to x square in the form x square minus k equal to 0, I got a quantity. If I can find its roots, well the root will be this square root of the given number k. So, the function f x in our case is x square minus k. What is f dash x? 2 x derivative of x square minus k is 2 x. Having seen this, I start with a value of x gas. Some arbitrary value has to be started. In this particular case, it can be mathematically proven that the value 1 will always go towards the root, if it is used as a starting point. Please note the difficulties in real life. If you have a complex function, let us say polynomial of order 20, you have no clue on what the initial value could be. What could happen? You could start with an initial value and the function could be behaving such that your solution will never converge, but you will diverge away and away from the root. It all depends upon the behavior of the tangent. So, whether a particular iterative solution will converge or will diverge itself is a question mark, which starting point to use itself is a question mark. These are issues which are addressed more properly in a course on numerical analysis. Some of you would be doing these courses in your, in your subsequent syllabus. Here, the objective is if there exists a numerical method, how to apply it and how to write a program. So, you come back to our problem. I compute x nu as equal to x gas minus x gas square minus k divided by 2 star x gas. Are you clear why this formula is? Are you clear how this formula has been derived? It is the same length that I want to compute and this is simplified as x gas plus k upon x gas by 2. This is just algebraic x part. So, this is what I want to calculate. So, the block of statements to be iterated repeatedly are identified as since x nu is the modified value of x gas to be used as x gas for the next iteration. The instruction which we can repeatedly execute instead of saying x nu equal to x gas this, this, this and then saying x gas equal to x nu. I can simply say x is equal to x plus minus something. Notice that is what you do when you are summing up a large number of numbers. When you say sum is equal to sum plus something you are in every iteration you are modifying sum reaching to the final value. This can be treated exactly. But once I have written an instruction which says x is equal to x plus k by x by 2 then I have a formula for iteration. I can keep repeating this starting with some value of x. I know x will change in every iteration. I know x will move towards the root. I can continue this endlessly. When should I stop? I have two ways of stopping one. I say I execute a fixed number of iteration. I say I will do it ten times or I will do it hundred times. That is one way. But depending upon starting value and the function ten times or hundred times may be such that I may never reach near the root or may be I will over do it. There may not be any improvement in the 11, 12, 13, 14 iteration at all. I have no clue. A better technique would be to say that I should define a criteria to stop when I have reached very close to the root. But how do you define a closeness? The closeness is related to accuracy of the solution. So, here is one way of defining the accuracy. Suppose this is my function and I want to calculate root correct to four decimal places. Now, four decimal places means if I am in this region I am ok. As long as I find out a root value which is not exactly this which this is the root. But suppose I find this point. Now, this point is within such vicinity of the real root such that the difference between f of x equal to 0 and f of x equal to that value is less than 0.000. So, that is accurate enough. I can of course, change this to 0.0001 depending upon how much accuracy I want. The greater the accuracy I need more will be the iterations required. But I leave it to the machine to figure out how many iterations it should execute by putting a conditional execution line. So, if I define closeness to be within 0.0001, 00001 sorry we wish to stop when f x is within plus minus this. This gives us a criteria. Set up an iteration, keep repeating the iteration while absolute value of f of x is greater than 0.0001. Observe the absolute value is essential because f of x may be positive or may be negative depends on the function. But as long as this criteria is valid I keep repeating. The moment the absolute value of f of x comes within this range I need not go any further that tells me how to terminate. All of this analysis results in a simple program which is shown. So, observe that this program defines k and x as floating point objects. It initializes x to 1 although in practice you will rarely initialize the guess value in a definition statement like float. We will actually read a value because for different functions different starting point may be there. In fact, you may want to experiment the same program with starting point to see what happens. Now, it will be very messy that every time you edit the program change that initialization and do something. You would rather put an input statement give a initial value and then start with that. Then you read of course the number whose square root you want to determine. The beauty is just a two statement iterative block which will solve this problem while x star x minus k greater than 0.0001 or please note the or is two vertical bars or k minus x star x greater than 0.0001. Do you agree that this or condition will be satisfied whether I am in the positive side of the root or negative side of the root. The absolute value is being determined. We do not know how to calculate absolute value in any other way right now. So, we use this. While this condition is true that is while I am not within the accuracy limit keep repeating what x is equal to x plus k by x divided by 2. As I said k and x all the float 2 is an integer but 2 will be converted to float automatically by the rules of the game. Of course, those of us who want to meticulously write this might want to write 2.0 here which is okay and finally I output the value. Is it clear how simple a program is to find out the square root of a number. Remember I said I could also execute fixed number of iterations. This is another example of the same solution a same problem solved in a different way where I use the counting control by force state. Here I have decided that no matter what I will execute the iteration only 10 times whatever is the value of k that you give. It so happens that in most cases we will actually reach the root because the convergence of this solution process is what they call quadratic. That means error reduces in proportion of square at every state. So, it is a very fast converging method. However, the way to implement is what is of interest here. So, we are saying for I equal to 1 I less than equal to 10 I plus plus. 10 iterations are set up only thing in iteration is to be executed x is equal to x plus k by x by nothing. This number 10 is fallacious. I could also take a value from user like how many iterations you want to run say a value of n and run the iteration from 1 to n and n can be any number that you want of course a positive. So, is this clear this is alternate form of solution. I would just like to illustrate the use of force statement in a file like case. It is a funny usage, but it is interesting and because some books will mention solutions of this kind I would like you to know although I would strongly advise not to use such artificial jugglery. What is the jugglery? I am saying flow takes k give value of k c in k. Now, I said of a force statement wait there is the iteration count there is no iteration count. What does this force statement say for x equal to 1 that is the initial value we understand. So, it will start with x equal to 1 next it will check the condition what is the condition x star x minus k greater than 0.0001 or k minus x star x greater than 0.0001 this is a familiar condition. So, it will check this condition and iterate what is the body there is nothing inside the body. Ordinarily force statement will have some statements to be executed. So, what happens in such a case there is no body force statement does nothing goes to increment the control variable and go back, but wait there is no increment here the increment is shown by this x is equal to x plus k by x by 2. Notice that this was actually the body that we wanted to iterate around, but since there is only one statement to be executed in that body I can actually write it as the third specification of the force statement. So, I am using for in a vile like fashion I am using for in a vile like fashion just to explain this when I say for x is equal to 1 x star x is equal to this etcetera minus k greater than this etcetera here x is initialized to 1 before the loop. So, this one next the condition repeats the loop if it is true this is the condition there is no body. So, nothing is to be executed the increment x is incremented to reflect them. So, this is a perfectly legal and correct c plus plus program it will work exactly like what the vile statement would do would you all agree that the iteration implemented by using vile was more easily understandable by most of us. So, this is this is a tricky implementation I would strongly advise not to use it the only reason I am discussing this in the class is you will find unfortunately several books and several problems solved in this fashion. In fact, it is not uncommon to have some examinations in some university system where they will ask you to necessarily implement a vile statement of that kind using for I think that is ridiculous, but just for the completion of your knowledge I have described this. But with this background now we come to the main issue that we wanted to discuss the notion of functions in c plus plus function is a facility to compute value of any mathematical function to the specified values of some parameters. So, consider this quadratic this is general quadratic a x square plus b x plus c no we are not talking about solving an equation was talking about just evaluating. Now, suppose I have to evaluate this function at x is equal to 4 at some point in my program say fifth or sixth statement in a long program. Suppose later on some 128 statement I again have to evaluate the same quadratic, but for a different value say x is equal to 2.3. I will have to write the entire expression a star x star x plus b star x plus c and imagine if I have a order 10 polynomial or something. Now, instead of doing this instead of having to write two separate statements such as y 1 equal to something y 2 equal to something where every time I write a complete expression I would like to simplify the problem. Suppose I could write the instructions to evaluate this polynomial separately at some other place and make the computer go to that place. This going to computer with the request to execute that function is called invocation. Imagine that the function is here the moment I write a statement y 1 is equal to square a b c 4.0 what I am saying is go to this function. Obviously this is called square this is the name of I am invoking the function you know what is invocation whom do we invoke normally God say God help us here we are saying Mr. Function help us the function is standing somewhere else. This function there are different functions like large number of God's that we have we have a we have a God called quad Mr. quad help us. So, he says give me the necessary parameters so that I can calculate what are the parameters required to be passed on to quad the value of a should come the value of b should come the value of c should come and the value of x should come the way it is written is a b c are some locations which hopefully contain meaningful values and x will be 4.0. So, the order in which I write these values in actual values in parenthesis are called the actual parameters and I take these parameters go to quad quad will take these parameters copy them in its own locations do the calculations find out one result and come back with some value there will be one value which will be written by this one. Next time when I call it again it will go to the same code and will come back this time with a different value here imagine the usefulness of this facility I can modular is my program although the illustration is only for a simple function we will see that every large piece of software in this world is composed of large number of such models. So, functions are provided in C plus plus we write instructions to compute value of a function only once and such a function will accept parameters from us and it will calculate the final value of the function and return that value and the function can be invoked or called from within a program as many times as you want we need to evaluate the function a x square plus b x plus c for evaluating the value of a function what values do we need to know we need to know a we need to know b we need to know c we need to know x the actual value this is my main program with these values I would like to go to some function and as I said come back at the point where I want to invoke it this is the definition of my function called quail float quail in bracket float a float b float c float x inside the bracket inside this function float quail what I have written I have written the statements which will evaluate the value to be written it defines value as a float location and it calculates value as equal to a star x star x plus b star x plus c and after having calculated that it executes a statement return value. Let us just analyze this statement this this particular code that we have written please note this is not as a part of your main program this is written somewhere else separate you shall see exactly how and where we write it but let us understand the implication c plus plus will compile this function as a separate standalone code while compiling it it should not need to know what is written in your main program what is declared in your main program the locations declared in your main program for computational objects are different these locations are different what are these locations it will it will put a location for a it will put a location for b it will put a location for c it will put a location for x and of course it will put a location for value how does it know how many locations are required simple as many locations as are declared within this function how many locations are declared within the function only one float value and therefore I get this location reserved by the compiler but there are additional locations and how they come about they come about because they are listed as parameters in the function definition. So I have said float a float b float c float x and that is why a b c and x are locations which are created by the function actually what is important for the function is to know in which order the parameters will come and what is the type of the value it is adequate actually for the function to know that there are four parameters the first one is of type float the second one is also of type float the third one is also of type float fourth one is also of type I could have parameters which is first one is integer second is float third is in fourth is care the function needs to know which are the place holders it has to assign these names a b c x are actually irrelevant except that they are used here the actual parameter. So in a nutshell what the c plus plus do whenever it encounters a function definition which is necessarily written as a separate block of statements a it creates memory locations for all the objects that are used within that function plus all the objects which are listed as parameters more importantly the specification of the function also tells c plus plus that after doing all the computations whenever this function is called there is a single value to be written it knows that value will be of the type float. So the type of return value is represented here it is important to understand the significance I can also say return I can write an expression it is incidental that I am returning a value which was declared as float but I can write any expression here four star p imagine p was integer if p was integer what would happen p multiplied by four will also be an integer but it will necessarily be returned as a floating point value because of this declaration attached to the name of the function. So it is very vital that every function name of my choice is preceded by the definition of the return value that is why the function is called float function int function care function or what these are the rules you can read them later but just to glance over them the first word in the function definition is the type of the value which will be returned. So I say float is the first word the next word is the name of the function what is the name that we chose why this is followed by one or more parameters and the parameters in our case have been float a whatever. Notice note that the order in which these parameters are listed is extremely important because it is in this order that the actual parameters will be brought in and copied into these locations if I mess up with the order I will get something wrong the return statement sends back the value and in between are all my computations is this followed pretty simple write a separate function quad this is the way the function would be invoked as I said C plus plus compiler will translate such functions separately it will keep these as compiled set of instructions in fact the process of coupling such compiled functions with your own program which is compiled is called linking process I link. So I take my translated program I take this function that function that function link all of them together that is what when you say C plus plus it does all of this it does compilation it does linking whatever. Now I have written the function if in my main program anywhere I want to evaluate let us say such a function f x which is given by 23.5 x square plus 9.7 x plus 12.6 that is the function difference and suppose somewhere I want to calculate the value by using a formula what is that formula 574.6 multiplied by f of x plus 1 upon x this could be a very well a formula in my computation now wherever this f of x comes I will have to ordinarily write the complete definition of f of x but if I have written a function separately called quad function then I can write my main program as shown here I can declare pro text n y then I can input x and I can calculate y as equal to this. Observe that what I have written here is an expression normal expression that you will write in C plus in that expression one of the elements happens to be the invocation or calling of the function quad. The other parts are one upon x 574.6 standard parts what the machine does is how will it evaluate this function by this expression by the way it has to calculate this and it has to calculate this and then it has to calculate this plus this agreed but when it has to calculate 574.6 multiplied by this it suddenly realizes that so called this is not a value is not a location from which I can pick up but it recognizes it says quad so like God I go to that function Mr. quad here are the parameters 23.5 9.7 12.6 and the current value of x please take these four parameters the quad as we saw will take these four parameters and will copy them into its own internal location a b c x will do the calculations and after calculation it will return a value that return value will come and replace this entire thing whatever comes back from the function. Once a value comes the expression is evaluated using its normal rules is that clear so within a program a function is invoked simply by writing the function name within any expression so please note a function is invoked within a main program by writing its name and the actual parameters that you wish to use for calculating the function value inside any expression since an expression can also be a standalone statement in c plus plus I can just write quad something something semicolon that itself will become the value but it will be useless to just write quad something something because return value you have to do something suppose you want to print out a value you can directly say c out quad something that will calculate the value and return what is important to note is that the given parameters are copied to the respective location there are other methods of parameter passing most important being the parameter passing by reference but we shall talk about it at a later date for the time being we will assume that the standard rule in c plus plus is copy all the actual parameters into the token parameters that you have in return inside your function definition. So, those are separate locations these are separate locations once that is done the function code is executed one value is calculated the type of that value is defined when you define the function and that will be return now the question if I can write a function quad I can use it in my program can you use a function which I have written yes there are two ways you say further please give me that code. So, I will give you that code you will cut paste and put it in your program and run the other one is I will say I have compiled that quad already. So, you say alright show me in which directory is that compiled code I will get my program compiled and tell c plus plus to link your compiled code also with my program. So, I do not have to do anything I will say fight, but you need to know how that function has been defined. So, that you can use it appropriate indeed such a thing is possible and indeed the main power of any programming language certainly that of c plus plus comes about because a large number of people have written a large number of useful functions which all of us can utilize. There are two types of libraries one which are application specific library for computational problems there are different routines which let us say people in our mechanical engineering department would have written all their students and others could use those functions, but there are standard libraries and these standard libraries provide a whole lot of useful functions which all of us can use. For example, there is a standard library called c math if you say in your program hash include c math then suddenly the following functions can be used in your program sign, cos, tan whatever you realize all the log all these exponential all of such libraries and their component functions which have been predefined in those libraries are defined as a part of c plus plus report on whenever you get a c plus plus compiler typically you will get all these libraries. When I write hash include c math you will recall that we normally write in our program hash include std, std i o what is std i o it is actually a library it is a library of functions which handle input and output. There is an std lib called c std lib that there are n number of libraries available and as we proceed further we will increasingly draw upon pre written functions to do our work rather than we ourselves writing those long programs that is not the same. The last one now it is time to understand why we say int main and why we say return 0 you observe that we have been writing it mechanically so far just as we can write functions give it parameters and get a return value c plus plus says that your entire main program is also a function its name is main that is defined by c plus however within a main program we may call functions who calls this main program itself the operating system when we say dot slash a dot out we are actually telling operating system please run my program the operating system treats this entire program as a function and that is why it has a return type called int and it has a pair of parenthesis so far we have not been writing anything inside this later in the course we shall see that we can actually pass parameters to the main program you know how the parameters are to be passed if on my command line when I say dot slash a dot out and I press return enter but instead if I say four three two spaces this four three two spaces this four and three become parameters the operating system says ah somebody is giving me two parameters it will take these two parameters and give it to your main function and it may use it in any way it wants we do not bother about it how it is to be done etcetera etcetera the definition is slightly clumsy we cannot understand that definition unless we understand the array structure we shall do that short the point is when we say return 0 what is happening is that the main program is returning a value 0 to the operating system because operating system called it so it returns the value 0 what operating system does with it we do not we have so far only stated that in general an operating system will treat a 0 return value as always well and if it gets 1 2 or 3 to operating system it means something went wrong but we do not know yet how to use that feature in any meaningful however the main program itself is a function is that understood we close this lecture with an example to evaluate a formula v is equal to square root of m plus square root of n divided by square root of m plus n we agree that such formula will come in eva for evaluation ok what do we do well we already written a program to calculate the square root can I not rewrite that program as a function and use it let me choose the name s root for that function how many parameters s root will have 1 because it has to calculate the value of only a given number k it starts with initial value if you want you can write a square root function which has two parameters with an initial of a guess also but ordinarily just the number whose root is to be calculated is sufficient so it has one parameter and this parameter will be a floating point suppose I have written that s root function with one parameter then v is equal to this will actually evaluate this agreed I am invoking the s root function which I have written separately how many times 3 times within the same expression perfectly value first time I go there with the value of m come back with the result next time I go there with the value of n come back with the result third time I go with the value of m plus n observe that while writing the function definition since I am defining the place holders or parameters I have to write names in a and b what but when I am invoking I am actually required to send an actual value as a parameter to that and therefore an expression is perfectly fine c plus plus will evaluate this m plus n whatever is the value so the total m is 5 n is 13 so m plus n is 18 and you take this value 18 and give it there and say to find the square root n come this is the way I will write my function please note that all functions are ordinarily required to be written before you write your main program so after writing include i o stream and using namespace std I should not write int main something I should first write any number of functions that I want to write we shall see how exactly this can be obligated and you can actually write the functions at the end as well but this is the standard when the standard way I write float s root in bracket float k what will this do now this will create a location for k it may create additional locations whenever it requires it will also create a location for x you will agree that the program written is exactly the same that we wrote to find the square root of a number exactly the same while this condition do this and it will return the value having done this this is my main program I declare m and n as two values v as the final value which I want to calculate collect the values of m and n then I invoke this function three times calculate the value of v and output the value it is not necessary to have a separate variable v if I wanted to simply say c out you agree because it is an expression and in c out I can write any expression it will be evaluated and the value will be calculated I can invoke the function inside I would like you to just quickly grasp through this program this program finds out the averages average marks scored by two groups of students students who attend CS 101 in slot five you are slot five right and students who attend lectures in slot 11 let us say as a teacher I am just curious to find out which group is doing better same test is given to everyone so what I want to do I want to read the number of students and their marks in slot five number of students and their marks in slot 11 and calculate the average average will be a floating point number just to make sure that I have a calculation precise up to many more digits because I expect both the groups to be very comparable so I declare the slot average as double it is not required to be double double means instead of 32-bit floating point it will be 64-bit floating it can be just a float that is good enough actually okay observe what I am doing I am reading the number of students in slot five call it n five I am reading the number of students in slot 11 I am calling it n 11 now I calculate slot five average is equal to average in bracket five comma n five so average is obviously the name of my function and I am sending two parameters to it one is the slot number and the other is the number of students in that slot but wait this is supposed to calculate the average should I not send the marks but marks are too many so what I have decided is instead of sending the mark because I do not know how to send let us say 250 students or 220 students take the test so how do I send those 220 names it is unclear right so what I will do is I will read the data for that slot in the function it is perfectly fine and once I get back the slot averages if slot five average is greater than slot 11 I say slot five has performed better or else I say slot 11 has performed better simple how is the function this is the function written double average as I said instead of this it could very well be flowed there is no problem observe the definition of two parameters int slot int n so slot number comes in and n which is the number of students in that slot I am defining marks as double sum as double everything as double for i equal to 1 to n I set up an iteration n is the number of students given to me so for each student I will read the marks and add it to sum so I start with sum equal to 0 and keep on saying read marks and sum plus equal to mark I will iterate this n time I get the sum I divide sum by n and calculate the value which is to be written directly so you think it is correct this function will work correctly only difference is we agreed some time ago that is c plus plus you should be writing functions before you write the main program but in this case we have first written the main program and then return this function this is not correct whenever you want to do such a thing c plus plus requires you can write your functions at the end but at least before you start writing the main program tell me the prototype of the function what is prototype what is the name of the function what is going to be the return value and how many parameters you are going to have in that function whenever you write it more importantly what are going to be the types of values of those parameters this is called a prototype definition let us go back to the previous slide and see the prototype double return value average name of function two parameters int int no name is given here because name is not important at this stage for c plus plus to compile your main program it only needs to know that if it encounters average somewhere then what is to be interpreted for this value and this value this prototype definition is important for another reason why consider the standard library those functions sign cost you are not writing somebody has written technically by rule I must write those functions before I write the main program but where are those functions when I write include c math what is included is not the complete code or program implementation of that function what is included is prototype definitions in a set of files called header files so some of you have done c or c plus plus programming earlier would know something called math dot h this used to be a header file which contains the prototype definitions of all the functions which are defined in the math slide where you include that all prototype definitions will come since your include statement comes at the beginning all prototypes get included at the beginning then you write your main code and the prototype definition requires only this and nothing so if I execute this program I will get something like this so let us say I give this is a sample test so I run it with three students in slot five two students in slot eleven I give some marks these are of course arbitrary marks and it so happens that the average gets calculated and I conclude that slot five has performed and you can you can do that for any number of students of all anyway so have you understood how functions are written how they are invoked and how convenient they will make our life particularly with the knowledge that large number of standard functions have been pre written by a number of people and c plus plus has these functions included in the standard library those of you have access to net can go to a c plus plus tutorial site and ask for standard library functions you will get all the standard library functions listed what are their parameters what they return etcetera etcetera and of course we will be discussing specific functions as and when we get to require we will close with some announcements any questions before we proceed by thank you very much