 Good morning everybody. Today, we are going to discuss functions, but to begin with I will complete the discussion that we had left yesterday on the iteration. So, what I propose to do is to complete the discussion on iterative solutions. Basically, revisit the problem of finding roots of a given function and look at different ways of prescribing iteration. We will then describe the notion of functions, the need for such functions, the way we define functions and the way we use these functions. Workshop projects or workshop activities is an important aspect on which there seems to be a lot of confusion. So, this is what we had begun discussing yesterday, the Newton-Raphson method to find the roots of a function that is find out x such that f of x is 0. And we had said that the method will work if f of x and its derivative f dash x can be easily calculated and a good initial guess is available. As an example, we decided to find the square root of k. Incidentally, I had just said use f x is equal to x square minus k. While you and I can understand that easily some of our students might have problem in saying where does this x square minus k appear from. So, I have added a small explanation as you can see on the right corner. I say let x is equal to under root of k, then x square is equal to k and x square minus k is 0. Essentially, this is how we arrive at the function f of x is equal to x square minus k. And of course, f dash x is equal to 2 x is easily derived from this. In any case, we observe that f x and f dash x can indeed be calculated easily. Only few arithmetic operations are required and we can start with an initial guess x 0 equal to 1 which in this particular case it always works, it can be proved. So, we looked at the method that we will follow. Essentially, the iterative method of determining the root will begin with some initial guess which we call let us say ith value of x where i is equal to 0 or initial guess. And our job is to keep finding a better value of x. Naturally, if this is the nature of the curve and it crosses x axis at this point, then clearly this is the root of the equation. So, we want to determine this value of x. Now, this value of x will have to be approached iteratively starting with this value. And what we are looking at is how to tell our students how we can build an iterative method to do this job. So, we observe that we have a point A which is the initial guess, its x coordinate is x i which is our initial guess, its y coordinate is 0. So, we conclude that point A is known. Now, we determine function of f x at this point. Now, this function evaluation will essentially get us the value of the function at this value of x. Let us say we get another point B. The coordinates of B are x of i that is the x coordinate and the y coordinate is nothing but the value of the function. That is why we say point B is x i comma f x i. And we now say it is known. That means it can be computed. So, once we assume an initial value x i, we can compute f of x i. And therefore, we can know the position of B. Now, we say we will approximate f by a tangent. Now, tangent at point B which is drawn on the function, it will intercept the x axis at a certain point. Let us say this is the intercept. Now, obviously you will notice that this intercept crosses x axis at this point which is clearly closer to the root. Obviously, because we are approximating the slope of this function by this tangent and as the function approaches the x axis, so will this tangent. So, that is why we can see that if we can use this as the next iteration point, then we have made an improvement over the original case. And successively, we would like to find such points by repeating the process. Anyway, to complete the discussion of this particular computation, we have calculated a coordinates of a which are simply x i minus x i comma 0, x i being the initial guess. We have computed the coordinates of point B by using the function evaluation as shown here. Then, we say we approximate f by the tangent c which is an intercept on x axis whose coordinates, let us say are designated as x i plus 1 comma 0. Our job is to determine what is the value of x i plus 1. It is for this purpose that we denote this as point c and show that the coordinates of c can be calculated by using a formulation of this kind namely f dash x is essentially the slope of this tangent which is a b by a c. So, we say this slope the a b by a c can be computed based on the coordinates that we now know. So, a b is simply the value of function f x i, a c is the difference between x i plus 1 and x i. So, the ratio of f x i divided by this is actually the derivative. Since the derivative is computed independently, we can by implication find out the value of x i plus 1. In this case, it simply amounts to x i minus f of x i divided by f dash of x. So, now you can see that I can set up an iterative computation in motion by starting with an initial guess x i, calculating f of x i and f dash of x i at that x is equal to x i using that calculate another value for x and then take that value as x as initial guess and then continue. So, this is the iterative process essentially then we consolidate these discussions in this slide x i plus 1 is x i minus f of x i by f dash x i, f of x is x square minus k and f dash x is 2 x and therefore, x i plus 1 is equal to x i minus x i square minus k upon 2 x i which is nothing but x i plus k by x i divided by 2. So, we conclude that starting with x 0 equal to 1, we can compute x 1 then x 2 and so on. Each successive value of x i will be closer to the root. We also note that we can get as close to square root k as required by carrying out these iterations many times. Suppose I carry out these iterations a million times, 2 million times, are we sure that we will get a value of x for which f of x is exactly 0? Well that is doubtful and that is because the computations are being done using floating point and errors in floating point computations were visited by us briefly which tells us that we may become very close to 0, but we may never get exact same. In fact, that is something that we should emphasize to our students right from the beginning that computational solutions to such analytical problems will yield approximate results which may be very close to the exact result, but you may never get the exact result as you may analytically get. Additionally, there could be a problem of stability of our iterative process, but then that is the topic for advanced discussions in numerical analysis, we will skip that. So, this in a nutshell is the method of finding out square root of k. Notice that the notation that we have used here x i plus 1 x i etcetera etcetera is to indicate successive iterations. In a computer program we do not know how do we describe x 0, x 1, x 2 these are not different variables. We can use only one variable and therefore it is important to tell our students that we start with one value and we evaluate successive values. So, notice that we have used only one term here successive values of x i and then we say that in our program we will simply use a variable x which represents the current value of x that we consider. Consequently, our program segment which will implement this I call it a segment because I have done away with the mandatory lines which always appear at the beginning of our program like hash include and int function whatever whatever int main etcetera etcetera. So, calculating square root of a number k start with a variable called k defined as floating point value because that is the value whose square root you want to find out. I input that value then I define float x i equal to 1. Notice that i is only an honorific attachment to x it has no significance. Indeed when I write x i like this in a computer program the two characters together simply define a male like any other name a b c alpha beta whatever one. So, x i is merely one name represents one variable which is a floating point variable and we initialize that variable to a value 1. Incidentally, we may use such an opportunity to introduce a concept namely initialization during declaration and then explain to the students that ordinarily I could have said float x i semicolon followed by x i equal to 1. Instead I can combine the two and provide an initial value to the variables which I declare during the definition of those variables themselves. We now proceed ahead we consider this to be an initial guess we comment that it is known to work the iteration itself which is set up is extremely simple. So, look at what we are doing for int i equal to 0 i less than 10 i plus plus I write a comment saying I am doing 10 iterations what am I doing 10 iterations for I use the present value of x i to calculate x i plus k by x i divided by 2. Remember this is the formula that we had derived for finding out the next better value of x. Consequently we assign this value the value resulting from the evaluation of this expression to our x i itself. So, we begin with some x i we modified it here when this square this brass is encountered the computer will go back here and re-e-trade and therefore it will successively use the new values of x i that we so compute. At the end of the 10 iterations whatever is the value of x i we will print it out. So, we notice two things one the program is pretty simple to write once we have understood that derivation and the identification of the block of statements which need to be executed iterative. Having done that we expect that in 10 iterations we should get close enough value, but can we be sure can we not write something which will check whether we have reached very close or not after all the root may still be further away. Essentially what should we do then we should examine whether we have reached very near the root or not. How can we do that? Let us look at a piece of paper to understand that. So, effectively so this is the point with which we started. Now what we are seeing is by using the method of the successive approximations like this I may come to this point then I will come to this point etc etc but after 10 iterations how far away I am from this particular point is not very well known. I may be somewhere here or I may be somewhere here only. You will agree that if this is the 10th iteration value that I get it will not be close enough to the root. If this is the value I get I will be closer. Strictly speaking how do I define the closeness of my final value with the root? That depends upon the difference between my final value and the actual value. I do not know the actual value so how can I compare? Well I can compare not the value of x with anything because the final thing is not known but I can compare the value of f of x. Notice that as I approach the root from this side the function is approaching the root from this side. So, if I calculate the value of the function at my final value of x at this point it will be this value. At this point it will be this value. Notice that at the root the function value should be 0 and therefore what I can do is I can find out the value of the function at the value of x that I have and see how close it is at or near 0. So essentially I have to compare the difference between f of x and 0. f of x should be ideally 0 but it will not be exactly 0 so how close it is is what I want to compare. Since in general I may approach this either from the positive side or from the negative side I should actually calculate the mod of this difference. Since I know the difference will not be exactly 0 I can put an error limit and thereby conclude that my calculation of the root is within this error specification. So, let us go back to our slides. So, we use this to calculate the root in a slightly different fashion. Here is another way. Here I start with the definition of x i and k as earlier float x i comma k. I now say c in k and then instead of initializing the first value of x i I do it inside the for loop. So, notice what I have said for start with x i equal to 1. So, I know the initial value but when I use such things in for loop the iteration condition as well as the increment has to be prescribed within the for statement itself. You will see that I am able to do all of this within a single for statement. So, let us say what am I trying to do here? I start with x i equal to 1 same equal this is the first part of the specification. So, I say initial guess known to work. After this semicolon I am supposed to write the condition which must be met for which I will continue iteration. The moment that condition is false I am supposed to quit iteration. Earlier we had written the condition in terms of count because we are doing 10 iterations. Now the condition that is being put is different. Look at what we are saying x i into x i minus k is greater than 0.001 or notice this is the or symbol or k minus x i into x i is greater than 0.001. Why we are writing these two conditions and that is because x i square minus k may be positive or negative. So, what we are saying is either this or k minus x i square basically we are calculating modulus of this particular expression. If this is greater than 0.001 what does it mean that the function value at the calculated point x is away from 0 by this much margin. What I am trying to say is as long as you are that much away from the root please keep reiterating. So, notice we now do not care how many iterations it takes 10, 15, 20, 2000, 200,000 whatever. All that we are saying is for every value of x calculate the difference between k and x i square. Ideally both should be same that is when we would have found out the correct root as x i. If they are different then we are saying as long as they are different probably more than 0.001 please continue iteration. So notice that in the first statement the initial value is given here the condition for continuing iteration is given here and after the iteration I am supposed to calculate the new value of x with which I want to continue. So, curiously in the third part of the four specification after this semicolon where I will ordinarily say count equal to count plus 1 or i plus plus or whatever instead I put this statement x i is equal to x i plus k by x i divided by 2 semicolon. This is the power of the generalized for loop namely the last part of the four specification need not be a mundane increment to a single variable. Notice that when we describe the structure of the for statement we said this could be any group of actions which must result hopefully into modification of one of the loop variables otherwise the loop will become infinite. So, inside the for loop we are beginning with the specification of initialization we say it is an initial guess known to work then we prescribe a condition for continuation of the iteration based on what we want to finally achieve that is until error in the square is at most 0.00 and if the iteration is executed what do I want to do next I want to continue calculating with a new value of x i which instead of calculating in the body of the loop I am specifying here as part of the for statement itself x i is equal to x i plus k by x i whole thing divided by 2. Having done this I notice that I do not need a body of the for loop at all because there is nothing else to be done curiously this is a complete and correct for statement initial value condition to be checked for continuing with the iteration and at the end of iteration what is to be done. So, actually what the for statement will do because of this semicolon notice that there is no brace here what the for statement will do is it will start with x i equal to 1 it will evaluate this condition initially obviously it will meet this condition the value difference will be greater than 0.001. So, it will say let me execute the body of for remember the w w w which we had denoted the body of for statement it will come out of this to examine where is the body it will find the body is null it will execute that null body means it will do nothing and will go to carry out the increment that we have prescribed in increment we are actually calculating the next value of x i after calculating this increment as per the specification of the for loop execution this condition will be again evaluated if this is true again I will go out see null body execute nothing come back do this increment again check the condition. So, I am specifying the entire iteration as a single for statement is actually a beautiful construct people might find it confusing you might therefore want to ask your students to rewrite this in the conventional form by putting this condition inside but by putting this increment or computation outside and by continuing may be detailed now when you do not want to iterate a fixed number of times and still want to use the for statement for forcing iterations this probably is the best way of describing thing after doing that you simply output the value of x i you will notice however that for statement which we said is meant primarily to execute a body of statements for a fixed number of times does not appear to be very logically appropriate for this type of activity iterative activity where our iterations are not bound by a count they are not to be executed for a defined number of times but they are to be executed while a certain condition is true as long as the condition is true I was to keep iterating this there is another way still another way of specifying iteration in C before that we will just describe the special ways of using the for loop whatever I just described is discussed here for x x x semicolon y y y semicolon z z z the body is w w w this was the specification of the for statement we saw yesterday in the alternate that we just notice the computations required for each iteration are all specified as part of the specification of for statement itself thus the body of statements w w w is missing because it is not required incidentally you may want to introduce another variation of this structure itself where we simply say for nothing semicolon nothing semicolon nothing and then w w w this curiously specifies an infinite iteration and the loop must be broken by some condition within w w w since we do not know how to break out of a for loop except when the for determines that a condition is over we will have to later on see how can we do that neatly indeed there is a break statement which we shall discuss later which can permit us to come out of the loop from within the body of the w w w statements that we see this is the other way that I mentioned I say float k I take as input the value of k I define my iterative variable x i and say float x i is equal to 1 now look at the iteration that I am describing now I am not using for I am saying while x i square minus k is greater than this or k minus this is greater than point zero zero one do the following iteratively what is the following x i equal to x i plus k by x i slash two indeed this is a far better way of specifying iteration when the iteration control is based on a condition to be satisfied rather than any count this is intuitively clear to any reader while this condition is true execute the body as many times as required till the condition remains true when I come out of this iteration I will come out only when the error is less than zero point zero zero one and at that point I can print the existing value of x i so this is a very neat way of setting up an iteration which is based on checking a condition which is not necessarily a counting condition it is useful to one of our students that extra care must be taken to ensure that sometimes this condition will become false because if the condition never becomes false it always remains true then I will have an infinite iteration that means our Dumbo computer will never come out of this iteration in this particular case we are reasonably sure that our algorithm will find out the root within this error range and therefore this algorithm is definitely workable this is a formal definition of the wild statement wild condition loop body so notice that loop body is nothing but w w w that we use in the description of the far structure so what does it do the computer first checks the condition if the condition is true then executes the loop body and keeps doing the same so repeat this simple specification iterative specific check the condition if condition is true execute body again go back check the condition execute the body check the condition execute the moment the condition becomes false comes out and execute another statement following the loop body if the loop body is a single statement then we need not use brasses this is incidentally true for false statement also is also true for if statement incidentally as teachers we should emphasize that putting brasses is recommended always why because later on if we modify the program and we decide that in a certain block which was originally only one statement we need to add more statements then we may forget to put brass there and the entire meaning of the program may change so since we may do such forgetfulness we should put the brasses right at the beginning this is a good practice finally a brief word on comparison between for and why if there is a control variable with initial value update rule and whose value distinctly defines each loop iteration such as a counter use for also if loop executes a fixed number of times although there may not be a variable used in computation but it executes a fixed number of time like execute ten times etc. preferable to use for in all other cases it is preferable to use why there is a third construct in C for prescribing iteration and that is called repeat until the only difference between for and while and repeat is that in both for and while the condition that we have prescribed is checked before even the first execution of the loop body is carried out that means it is possible that if the condition is false at the beginning the loop body may not be executed even once in case of a repeat statement where we write the word repeat then we write the block of actions to be iterated and then finally we say until such and such condition the condition is checked at the end of the loop and if it is false we come out with this background we now proceed to discuss the notion of functions consider that what we found out the root of a equation just now consider that we want to find out the root of a more general function let us take a quadratic or it could be a polynomial or any other function we consider as an example a quadratic function a x square plus b x plus c in this case f dash is equal to 2 x plus b now if I want to redo or rewrite my program of Newton Raphson method to calculate the root of this equation then I will have to make changes in my program at places wherever I was doing calculation of f x or f dash x as a matter of fact you will notice that in this example we are simplified the ratio of f x and f dash x and we had actually got a uncanny formulation which was neither f x nor f dash x let me go back a few slides and tell you what we are done there look at it this way we were calculating x i is equal to x i plus k by x i whole thing divided by 2 where did this come from is this the definition of f of x no is this the definition of f dash x no what we did is in the Newton Raphson method we found out the formula for x i plus 1 to be computed from x i using f x and f dash x of course and then simplified it as a result the function or its derivative evaluation does not occur explicitly here at all now suppose we go back to the slide that we were discussing namely the function definition suppose I want to use my program but for calculating root of a different function as I said in this case a quadratic a x square plus b x plus c or 2 x plus a b which is f dash x there is no easy way by which I can simply replace some statements in my program and fit the requirement of this quadratic function in the Newton Raphson method so we say that it would be nice if we had separate blocks of instructions to calculate these for different values of x so whatever is the function and its derivative if I had written a code to calculate the value of the function and code code meaning program instructions to calculate the value of the derivative at a separate place then it would be very easy for me to replace those statements by some other statements representing another function this is precisely the purpose of a function in C which represents such a separate block what a function does is that it takes one or more parameters and returns a single value of a specified type that is the objective of a function here are examples of functions the examples represent exactly the same functions that we are considering namely a quadratic equation and its derivative so here I have said float my function in bracket float a float b float c float x then I have said float value then I have said value is equal to a into x square plus b x plus c and then I have said return value closing bus let us spend a couple of minutes in discussing what this is it appears to be almost a complete program which it is in fact it has a declaration for a variable called value it has computations in the form of this expression however there is no input statement so there is no way the value equal to this expression can be evaluated because we do not know where are the values of a or x or b or c are coming that is the objective of putting these descriptions within the function itself inside brackets notice that this resembles very much like a mathematical function that we are familiar with in this case my function is the name of the function that I am giving float a comma float b comma float c comma float x are called four parameters with which I will call this function why there are four parameters because to evaluate the quadratic I require values of a b c and a b and c finally it says return value that means we sort of understand that this function will execute as a standalone entity somebody at the beginning must give this function values of parameters a b c x and at the end of computations this function will give that value to somebody who is that somebody will come to that but effectively we conclude that function appears to be a standalone program which cannot be executed just like that on its own but somebody else will say I want to execute this function whenever that somebody wants to execute this function he or she must give this function appropriate values of the parameters as described here when function computations are over the computed value of the return value will be returned to that somebody who called me to execute this function this is the way I can write as many functions as I wish I have written here float my function and float my derivative because for this particular problem I need the calculation of the function value and the calculation of the derivative notice that in the second function called my derivative I have only three parameters that is because the f dash x in the case of a quadratic requires only values of a and b and of course the value of x at which the derivative needs to be computed here the computation is similar but simple I describe float value again value is equal to 2 a x plus b and then I say return value so this second function appears to behave very similar to the first function except that it is doing some different computation some questions having said float y value here why am I saying float value again having said float a float b float c here why am I again saying float a float b or even float x for that the correct answer is and this must be communicated to the students very early that each function is to be regarded as a separate program and a separate program means that program does not know the existence of other program consequently when we write these functions if I write ten functions although I may use same names in fact with similar meanings the name used in one function has absolutely nothing to do with the name used in another function so these functions are completely independent of each other in terms of the computations that they do in terms of the variable names that they use in terms of therefore the memory locations which they indeed when our c dumbo looks at these functions it will actually compile these and will create separate memory locations for this a for this x for this b for this c so you will see when you save this float a float b float c float x etcetera are not only therefore definition of definitions of the parameters but it effectively also tells c that please reserve space for a b c and x even in the main program I am using the same names those names will have different locations these names will have different locations ordinary much later in the course when we describe the notion of passing parameters by address or by reference at that time we will clarify that situation could be slightly different but ordinarily a function definition is used in c to isolate a certain part of the computation and such isolation is accompanied by the corresponding isolation of all the variables that are used in that function so I repeat while I might be using a x b c etcetera etcetera in the main program from which I propose to call this function these names a b x and c are completely independent of any similar names that I might have used here not only there in my main program but even in any other function as can be seen so these two functions in fact are completely independent bodies of code having said this we will now look at how functions are to be used and what are the rules of invocation of these functions first the syntax notice that we have not used the word syntax so far if this is the style you wish to adopt in your teaching then it is good to formally introduce the word syntax somewhere and say that syntax means the exact grammatical rules using which you much must write various instructions or statements in the c program so whenever I want to write the function definition then I must know the grammar rules or syntax rules and that is why I am defining syntax of defining the function in my function float a comma something comma something the first word here int tells me that type of the value which will be returned notice we said function will receive some parameters calculate some return value calculate some value and return it that value itself could be integer could be float later on as we shall see it could be of some other permissible data types also how do we let the see the computer know what type of value will be returned that is prescribed in the first word you will recall that yesterday a friend of ours had asked the question that wide something is often used wide main so what is the purpose of wide the purpose is that if I have a function which does not return any value and we shall see why such functions could be returned we may simply say wide here in which case the return statement will not exist inside exist inside that function so here I am saying int my function float something something the first word there is that type of the value which will be returned incidentally I should not use the word int in the context of the example that we are discussing because the example requires us to calculate a floating point value a real value and return it so this is actually a mistake it should be replaced by float next is the name of the function which we choose appropriately I have said my function deliberately it could be absolutely any name f square root square quadratic polynomial my polynomial whatever what incidentally the naming convention in C permits you to use a very large number of characters to define the name there are rules of format forming names we have not discussed them in this class but these rules are prescribed in every standard textbook and it is part of the C standards in a nutshell the name can be as long as you wish but the first thirty characters of the name must be unique so in a program I can use names with very large number of characters and it is recommended that we use this facility to name our variables and functions etcetera appropriately so the name should reflect the meaning here I am saying my function therefore I called it my function so to recapitulate the first word describes the type of return value the second word describes the name of the function this is followed by a set of parenthesis in which a series of parameters are listed so this is followed by one or more parameters whose values will come from the calling instruction what is the calling instruction we do not know that yet but we understand that this function will be in hold by someone for some computation whosoever that someone is will be supplying the values the parameters is what we are saying then there are some computations in the body of the function and finally there is a statement return value this says what value is to be sent back in general it can be an expression so instead of value I can say a into x divided by y multiplied by z plus something whatever whatever it can be any complex expression that see programming language permits the expression will be evaluated based on the values of various entities involved in that expression at that point in time and whatever is the single resulting value that will be returned return to whom it will be returned to whosoever called this function for evaluating the value of the function the point to be noted is that while a function may collect as input to itself multiple values from different parameters as prescribed here it will always return a single value only that is the reason why the single definition of type is adequate in this sense it it works very close to or similar to the mathematical functions so when I when we say y is equal to f of x at x equal to something then it gives you a single value y could be equal to a function of x comma z comma a comma b comma c but we imagine that mathematically it will take all those parameters calculate a single value and return it which is what will be assigned to the left hand side that is precise the purpose the value which is calculated after all this and return is a single value and the type that is defined for the function is that single value here so we again reiterate this return value says what value is to be sent back it can be an expression which is evaluated and the return statement is executed there in our model of computing where does this fit since we are thought of our entire c programming language and the computer which executes c program as a dumbo we can imagine that each such function is to be evaluated by a separate assistant dumbo separate from our main dumbo so anytime a function is invoked within an instruction which is executing who is executing this our dumbo so I have a c program in which I am executing instruction one instruction to instruction three etc suddenly dumbo finds during the execution of fourth instruction that a reference to a function has been made a reference to a function will naturally involve the values of those parameters which are required to calculate the function value at that point in time however whenever a function reference is encountered what Mr. Dumbo does is that he hands over the given parameters to the assistant dumbo and says you calculate this function and come back to the assistant dumbo now takes these input incoming parameter values calculate the function value and returns the same to the main dumbo what does main dumbo do notice that he had temporarily must have suspended the execution of instructions at his end because when he encountered a function he was in the midst of doing some computation so he would have stopped at that point he has given the function evaluation task to assistant dumbo assistant dumbo completes the calculations and after completing the calculation hands over the return value to main dumbo main dumbo now takes that value and uses this value in place of the function reference so whatever was the function reference originally during execution it is now taken out and a single value is put in its place and dumbo calculates the computations or execution of the instructions from that point onwards so this is a reasonably easy and decent way to explain to the students of course in practice there is no main dumbo an assistant dumbo it is the same dumbo or the same computer which is doing all of this but by giving this kind of example we can help our students to mentally separate out the function evaluation from the execution of the main problem how do we invoke a function the function is invoked through a function call so within a program you have to give the function name with appropriate parameters within any expression wherever you require the value of the function in the Newton Raphson method we have the value x i and we calculate next value using this suppose our function was this then we could design our program using the two functions which we have written my function and my derivative why because my function calculates a x square plus b x plus c and my derivative calculates to a x plus b which is nothing but the derivative of f f of x f dash consequently let me go back to slice and show you those functions once again these are the two functions which I have written float my function and float my derivative exactly these definitions float a float b float c float x as parameters this computation followed by a value being written in case of a derivative parameters are a b and x computation is this and at the end the value of the derivative will be calculated and it will be written so the function definitions are complete having defined these functions let us go back to our slide now what we are saying is that in order to implement Newton Raphson method using these functions I simply could use my function and my derivative wherever I need to calculate these functions here is the implementation of Newton Raphson method using function calls notice that the program now says int main as usual float x comma a comma b comma c comma root please remember these a b c x etcetera have nothing to do with a b c x that we used in the definition of the function these are now the variables of our main program for which number would allocate different memory locations to it I will read values of a b and c now the iteration is set up by starting value x is equal to 1 for int i equal to 0 i less than 10 i plus plus remember the first instruction the only difference now I am saying is in the body of the iteration I am saying x is equal to x minus my function divided by my derivative so wherever x minus f of x divided by f dash x was there I am replacing it by a proper call or a reference to the function that I have written it is interesting to very briefly dwell on what happens when this program is executed this is very simple reading is very simple initialization is simple we understand the first statement initially i is 0 since i is less than 10 I will actually go into the body of the execution the body says x is equal to all of this what will dumbo do what will computer do the computer will first ignore the left hand side remember what we said is first evaluate the expression on the right and then find the single value and allocate it to x so the computer goes to the right hand side it notices this is the beginning of an expression first element is x so you will go to the memory find out the current value of x which incidentally will happen to be 1.0 which is the initial guess first time then it sees a minus sign and it sees my function a comma b comma c comma x please note that this is not a multiplication this is not an addition this is not a value this is not a variable so there is no direct way for computer to get this it therefore understands that this is a reference to a function it will now go to that my function using the present values of a b c and x an important point to note is that if that function has been written to accept four parameters in a certain order then the actual values that are supplied to the function must exactly be four in number and must exactly be given in the same order so you will notice a comma b comma c comma x written as parameters computer will take these values which are here in this program at present so whatever the values of a b and c which have been read and the value of x at this moment is one so the input values and the value one will be taken by the computer and it will now go over to my function within the my function using these values it will calculate a return value which will be nothing but the value of a x square plus b x plus c at the given value of x now when the function finishes the computations it will come back with that value what Dumbau will do what the computer will do is it will replace this complete reference by a single value the value could be 2.175 value could be a 0.0124 whatever whatever that will be the value here in exactly the same fashion now the Dumbau will continue computation it has got x minus 2.12145 let us say then it looks here slash so division divided by what another function called my derivative in brackets a comma b comma x compared to exactly what it did earlier it will go to the function my derivative taking with it the values of a b and x will calculate as per the specification in that function called my derivative get a value bring that value back here and replace this reference my derivative a comma b comma x by that value so the simplified expression for Dumbau has now become x is equal to x minus 2.13 divided by 3.12 let us say meaning depends upon what value you have got now the expression is simplified the crux of this is a function reference causes an invocation of the function where the values are taken to the function which are given as parameters and it also causes a return value to come back and replace the entire function reference that we have made in the expression this then is the mechanism by which functions are invoked. We consolidate the invocation rules by revisiting the statement x is equal to x minus my function a comma b comma c comma x divided by my derivative a comma b comma x so when Dumbau encounters my function while evaluating this expression it suspense execution of the program first of all it says temporarily wait I cannot proceed unless I get the value of the function it goes over to the defined function with the available values of the parameters then it calculates the value executing given instructions within that function so it has suspended the execution of main program it has gone over there it has executed the instructions given in the function using the parameters which we have supplied calculating a return value and then it brings the value back here when the execution control comes back to our main program then we replace the reference to the function by the return value and continue evaluation of the remaining expression so it is extremely simple and straight forward as long as we understand that function is a body of code return and kept separately somewhere whenever a reference to the function is made computer is supposed to go there with parameters execute those instructions and come back with a single value and whatever was the reference to the function made in my main program that reference will be replaced by that single value that is how the invocation works a few points to ponder on this issue first we see that the calculations pertaining to our function evaluation have been separated out by writing the function separately perhaps resulting in a better structured or modular program why is this important we again reiterate what we began with suppose I want to calculate the root of some other equation that is we wish to modify the same program to calculate root of another function then it is far easier to replace code only in that part where functions are defined notice that the implementation of the Newton-Raphson method with the name my function and my derivative is generic now if I have a different function then I will take that function and its derivative rewrite the function definitions which I have given replacing them by the right computations but my main program continues to work well if we do not do that we may otherwise have to search our entire code in the main program to find out which lines we have to change if I have to change a function and as we saw the original implementation of Newton-Raphson that we did the value of the function and the value of the derivative was not directly visible in the code that we had written because we had simplified things there in short modular programming or structuring our programs like this where we isolate code which may have to change is an extremely good practice is another useful thing that we are emphasizing to our students when we use this kind of a here is something that a student may ask and if the student does not ask loudly in the class because these their students do not tend to ask questions we have to encourage them to at least think like that and preferably ask questions like so I may as a student now I may say can I use programming code for functions written by others now you have suppose somebody has a very complicated function definition and he has already written a program can I use that program yes of course that is the very idea in fact we can at this stage if such a question is raised and as I said even if the question is not raised we should actually invoke this question and we should say we can even compile those functions separately and link them with our program but we need to include prototype definitions of these functions within the program so that our program understands what I am going to call at any point next question what is a prototype well that is where we stop we will say the notion of prototype shall be introduced when we discuss separately compiled functions later in the course incidentally another point to ponder is we perhaps now understand why we say int main and returns hero in our main program notice I mentioned this in the as an answer to a question but it should perhaps now be related to the fact that our entire program is actually treated as a function by the operating system so when we ask the operating system to execute our program by saying dot slash a dot out that's the command to the operating system so operating system now is the main number the main number says dot slash a dot out and it says ah this user wants to invoke a function called a dot out so he goes to my subdirectory finds that executable code loads it and starts executing it as a function it need not pass any numerical parameters to that function because that's the main program written by some human being so operating system assumes that you would supply all the values as input or something however later on we will notice that whenever operating system invokes a main program as a function it does implicitly pass certain parameters to that in case of c they are called arc c and arc v we shall discuss these at a later stage at this juncture we need not worry about it we can just tell our students that ordinarily the operating system will merely invoke our program as a function whenever we say dot slash a dot out or whenever we basically ask the operating system to execute our program whenever our program terminates the execution the computer doesn't halt because in real life in the labs we have seen that when the program execution completes we simply get a command prompt back dollar who shows us that dollar not our program it is the operating system which shows us that dollar prompt consequently it is reasonable to assume that just as I have understood how to invoke function and get a return value the operating system when it treats my program as a function invokes it and at the completion of the conclusion of that invocation or that completion of execution of that program it shows me again a dollar prompt how does operating system know that my program has finished execution internally my program when executed through dot slash a dot out actually is invoked as a function it completes execution and returns the control to operating system while doing so it may optionally return a value in most cases it is advisable to return an integer value and that is the reason why all main programs which are incidently functions for the operating system as we see are generally written as in to main and generally an integer value is written the convention is if the value 0 is written the operating system assumes that the program executed healthily and concluded its job proper however if we encounter some error and if we wish to inform the operating system that there was a problem we can return some negative value such as minus 2 minus 7 plus 28 these are arbitrary values we must understand what operating system will do with these arbitrary values preferably we should ourselves prepare some kind of error reporting in the while the program is being executed but essentially as far as operating system is concerned it is invoking our main program as a function and taking back a value if the function is supposed to return a value of a specific type so with this I conclude this part of my talk if there are any queries I would like to go over to there is a query from JEC caucus I will go over to JEC caucus now to see what is the query JEC caucus over to you what is the take side of the function over to you I will repeat the question for the benefit of others thank you the question is what is the size of the stack of the function unfortunately that is not a concept which could make sense to first year students at this juncture but for your information the way the functions are invoked the parameters which are passed usually the operating system or even the runtime system of a programming language will use stacks on which you will push the various parameters it is not uncommon that in order to do context switching namely I am keeping my present program in abeyance and then going over to another program after which I have to come back and start executing my program in which case I may push variety of information about my current program also on a stack as far as the function is concerned the function stack is actually could be a growing stack depends on whether I am calling that function just iteratively or recursively if I call it recursively I will require a growing stack if I am calling it iteratively the stack will be limited the exact value or the number of locations in that stack are irrelevant we presume that the operating system will provide as much memory as is required for the function invocation from that stack thank you very much VNIT knuckle has a question let me go over to VNIT knuckle thank you very much the question is that the value of an integer which were defined yesterday I had defined it to be in the range of minus 2 to the power 31 minus 1 and he thinks that it is only in the case of positive integers not in the case of negative integers I notice that there is a similar question from my a view people here this is a question asked by a participant from PSG co-emptor if we declare an integer variable the largest value it holds varies from system to system what is the reason behind this also the question asked by our friend in VNIT knuckle which is is the range correct so let me go over to it again to a paper usually we allocate 32 bits for an integer in fact the exact number of bits allocated actually varies from compiler to compiler so first let me ask the question to our friend from co-emptor why on different systems the values are different the values are different because on some machines particularly smaller machines a integer may be allocated only 16 bytes of space a 16 bits of space here now obviously 16 bits will hold much smaller value than 32 bits in some cases such as large mainframes or supercomputers my integer may be 64 bits in which case I will have a larger value here still in some other cases where multi precision arithmetic is implemented and during the course of workshop when we discuss the arrays I am going to actually discuss a problem where I want to represent let us say 100 digit numbers or even 500 digit numbers these could be artificially done through software so there are variety of ways but as far as the basic data types are concerned yes the data type int and long end could have different definitions on different machines typically however an int is 32 bits now coming back to the question from our friend why is the value 2 to the power 31 minus 1 he thinks it is only in case of positive integers not in other cases actually what happens is there are two ways in which you can represent values let us consider the 32 bit representation the value can range the if I just look at the number of bits the bits can be either all zeros or the bits can be all ones these are two possible combination this will result in a value 0 and this will result in a value 2 to the power 32 minus 1 you can actually confirm it by looking at an 8 bit number if you have an 8 bit number then the smallest value you can represent is 0 and the largest value where you are all 111 and all the 8 bits the value is actually 255 which is nothing but 2 to the power 8 minus 1 so this is strictly speaking the range of values that you can store internally however if we did that we can never represent negative values so the C compilers actually permit us to use a signed value so that is why whenever we say int it is not an unsigned integer value that we are representing but we can represent positive or negative values both how can we do that so what the compiler does is it takes out one bit from this 32 bit and this bit is often treated as a signed bit technically most compilers use a two's complement representation to store negative values without going into those details also you can see that the remaining 31 bits now can have a value largest which is 2 to the power 31 now I want to represent positive and negative values so if I remove this signed bit I will remove this say one value from there so either the largest value is 2 to the power 31 minus 1 positive value and I can say negative largest value is also minus 2 to the power 31 minus 1 if I treat this as 0 to mean plus and 1 to mean minus so this is the simplest exploration that is the reason why the the value range turns out to be like this I do not know if in your syllabus if you are required to teach students the two's complement and other details then perhaps you will have to go through some additional material and additional examples but generally this kind of explanation is is adequate here is another question from GEC Triture how to return two values from a function at a time for example two roots of a quadratic equation an interesting question because the example that we took was that of a quadratic and quadratic will actually have two roots in fact a as simpler thing as a quadratic equation poses very curious problems it is not necessary that every quadratic should have two real roots it is not necessary that every quadratic must have two roots if the quadratic ax square plus bx plus c has b equal to 0 and c a is some value or say a is 1 as we saw we essentially reduces to square root which is a single value of course technically that single value can be positive or negative and therefore it has two real roots one on the positive side one on the negative side in case of a quadratic not only you can have two real roots anywhere on the x axis but you can also have complex conjugate rules the present Newton Raphson method sadly has no provision to handle that and such computations have to be handled differently coming back to the original question from our friend in GEC Triture can I make the function return two values the answer is yes and no no in the present form that we have returned the function and we have understood the function a function will only return one value that is the objective of a function even mathematically whenever we write some y is equal to f of x we actually have in mind a single value coming back it is not a set of values that the function returns yes because in real life there are situations that when we invoke a function we may want not just two but maybe a large number of values that is where later on after describing arrays and after looking at additional problems and their solutions we will comment on how a function can return multiple values the mechanism of returning multiple values is completely different and the return statement with which we associate a value to be returned can have only one value return and nothing more we are going over to NIT surat over to you thank you very much okay let me repeat the question for the benefit of AV users the question that is being raised by my friend from NIT surat is that how to call a function which is written separately by somebody else which is outside my program so we might use some examples of that at the moment let me tell you something whenever I use functions like scan f or print f these are proper functions by the way please notice that when these functions are called we are not defining them within our program they have been written by somebody else they exist somewhere else and indeed when we say include STD IO dot H we are not only including the header definitions of these but the entire function library is invoked by GCC system and those functions are linked to our program so a very simple and elegant way of describing to people is that the functions which we use as a part of the standard library functions say square root for example these are all functions which are written by somebody else they are part of the standard library in this case and they are invoked automatically whenever we call them in exactly the same fashion we shall see and I will include a example for your lab exercises somewhere where there are separately written functions which are compiled separately and which then can be invoked from within your program while you compile your program but while linking you link it with those functions also it is entirely possible thanks for this question I will include some examples in the exercises in the lab but the best way to tell our students is to say that look when I say print f or scan f I don't give the definition of that function although we now understand their functions so somebody else has written them they are part of the standard library and the C compiling and a runtime system automatically includes these compiled functions into our programs NIT Jalandhar is asking a question can the main function return a float value actually the main function can be can be written to return any value we can as I said most appropriate thing is to say void main because it is not expected to return any value the question actually is slightly ill founded because the purpose of returning a value from a function is that that function has done some meaningful calculations because somebody asked that function to do so and it has the responsibility of returning that value since the main program is in hold by the operating system and the control goes back to the operating system it is completely irrelevant as to what we return if you wish you can actually write float main it will compile but it will be a meaningless exercise because that value is not going anywhere the operating system is not going to do anything with it so we conclude by saying that while the main function can also return a value of any type since we do not know whom that value is returned to because we do not know the characteristics of operating system we do not know what will be done by that value and therefore it does not make sense to associate any value with the function main best bet is to call it void since we do not understand what void is now we can explain to our students that instead of saying in float or float if there is a function which does not have to return any value we can simply describe it as void something it tells the compiling system that this function will never return a value the question that now I would like to ask all of you is after all we discussed the notion of a function because we wanted to do some computation and return a value forget the operating system interaction with my program why would I ever think of writing a function within my program which does not return anything at all why would I think of a void function I would like all of you to think about it and come up with some examples I will include one example particularly a function which handles errors that means in a large program there could be errors that could be occurring and different modules will supply an error number like 1 2 3 4 5 instead of giving an error message to be printed or to be shown on the terminal by individual modules of the program it is not uncommon in a large software project to consolidate entire error reporting and invoke a single function to do that in that single function you will have variety of print f statements which will print some detailed message for error number 1 some detailed message for error number 2 etcetera etcetera. So, the only parameter given to that function is the error number which has been encountered somewhere else in the programs and what that function does is merely print an appropriate error message very clearly in such a case that function need not return anything back. So, this is one example of why a useful function may not return any value. However, the question about having to return more than one value is very pertinent and as I said we shall discuss this at a later stage. Thank you very much.