 Hello and welcome to the NPTEL course on an introduction to programming through C++. I am Abhiram Ranade. The topic for this lecture is program organization and functions and the reading is from chapter 11 of the text. So the central question that we are going to answer in this lecture sequence is how do we write large programs? Now designing or building anything large becomes easier if we can think of it as being made of small parts. So for example, a book is made of chapters and if we say the look, let me concentrate on chapter 1 now, then concentrate on chapter 2, then the whole thing becomes easier to think about. Say you are designing some object like a car, then a car has many subsystems and you do not design everything together, you do not have everything in your mind at the same time. But you say that look, let me now design the engine, let me now design the electrical subsystem and you sort of break it up into small pieces and work on the pieces one at a time or at least one person works on a piece at a time. For programs, such smaller units are functions. We break up the overall requirement into separate small somewhat independent computations and code of these separate parts as separate functions. Now we are going to think of a program as being made up of functions and we have already said that even our main program which has been looking different from functions so far is actually also just a function. So let me make that clear right away. In C++, the standard way to write the main program is to write it as a function. The function must be named main and its return type must be int. Why is it int? That is really for some historical reasons. The main program is not really returning anything but for some historical reasons it has stuck that its return type must be int. And for now the main program is not going to take any arguments. So instead of main program and a body containing appropriate statements, in C++ you really should be writing int main. So main which does not take any arguments and the body containing whatever statements. Simple CPP is just doing a translation for you. So when you write main program, simple CPP translates this main program into this text. There is a feature in C++ called pre-processor macros which are being used over here in case you are curious. But for this course you do not need to understand pre-processor macros but if you are curious they are discussed in the book. So why did we do this? So simple CPP provides this feature so that on the very first day you do not need to understand what is a function, what are arguments to a function and why is it int or should we care that it is an int. We did not want you to ask all these questions or we did not want you to worry about all these questions and therefore we made this thing called main program and which we were ourselves the simple CPP was translating into what is actually required. But now that you know functions, oh by the way the function main is allowed not to have a return statement. So this is a concession that is given for main because the value return is not of any consequence anyway. So we have not been returning a value inside the main program. Now that you know what the thing is really required in C++ we are going to stop using this phrase main program. So we will write int main and then the body and this is how we will write our main program as a function whose name is main. Your compilation procedure will still work if you are using an IDE if you hit the compile and run button or just the compile button that will work. If you are using the S++ command under Linux that will still work and I will encourage you to use int main because after all that is sort of what will be available if you go outside simple CPP and anyway it is not really a major convenience at this point to be able to write main program. Now that you understand what functions are. So let us come back to the question of dividing code into functions. So why does it help? So if you have a lot of work to do you would like to get many people to do it so similarly if you want to write a big program presumably you want to assign it to many people and certainly if you give different functions to different people then that is easier to is that makes it easier for people to collaborate. Another very interesting feature of functions is that they are self documenting. What do I mean by this? The function name parameters give clues as to what the function is doing. If you just write the code inside your main program then there is no such clue. So going back to the functions that we had if we write int gcd then the very fact that we have a name over here tells us that look something like a gcd is being computed and m and n are there so it says that look maybe the gcd of two numbers is being computed something like that. And here things are even clearer that a tree is being drawn or something like it something is happening to something involving a tree even that is better than just putting this kind of a code inside your program and of course if you are going to recurse functions are of course essential but even if you are not going to recurse just the fact that there is a name over here forces you to say something about what is being done. Of course all this works only if you are choosing the name nicely and we hope that you choose the name nicely we hope that you choose the names of the parameters nicely. So for example you really could say over here int level not just l I have been using level over here because I want to put this inside a slide and inside a slide if you put big names then it will not fit in the slide but really good programmers will use much longer names so you could even say instead of rx you could say root x if you want or x root whatever style you prefer. And when you write a function there is a natural expectation that you should write the preconditions and the post conditions. So we have written preconditions and post conditions here in a very compact manner and here in a little bit more descriptive manner in fact this last comment says a little bit more as to what area what specific area the picture is going to fill out. So what happens is that in a long main program it may not be obvious as to where as to where and how to put the comments whereas in a function there is a proper place for it, there is a natural place for it. And I cannot overemphasize the need to document your code as I said earlier your code will be written will be read by you later on and you would want to know what it does and believe me you will forget why you wrote some piece of code and of course if somebody else is going to read the code they will need to know why you are writing what you are writing. And so functions say that in this small piece of code this is what is going to happen. The names implicitly say what is going to happen and furthermore if you put in comments giving the preconditions and post conditions great. Functions also allow you to adopt this strategy write a little bit of code test it write some more. So basically you can test your program one function at a time you do not have to write the entire program and then test the whole thing. That is really tricky to do because you will not easily know where the error is if you write a small function and it makes an error it is much easier to fix the error. And finally a function is a good way to package and share a code. If you have written some code and you want to give it to someone else you can give them a file containing a function which contains the code. So a function sort of is a nice wrapper into which you can put code. So in some sense a function is a logical unit of code. The physical unit of code the physical units are files. If you want to give anything like code to anyone you have to give a file. And of course if that has to make sense it is nice to put a function in it rather than sort of code by itself does not make sense code just a loop by itself or just a declaration by itself does not make sense whereas a function is kind of a nicely packaged piece of code. But that is that sort of logical packaging physically you have to give a file. So that raises a bunch of questions. So suppose several people write different functions of the same program it is convenient for each person to use a different file. And so the question that is raised is how will a function in one file call a function in other files? So we are going to answer these questions fairly soon. Now you may be thinking at this point. Look in this course am I going to be writing large programs or is this discussion about large programs really not relevant for this course? So some answers are due. So it is true that you are not going to be writing very large programs in this course. So any program that we ask you to write will be at most 150 lines. Each of the time they will be fairly short so maybe there will be 20 lines. A 20 line program does not can fit in the file and it does not it probably does not need to be divided into small functions. On the other hand a 150 line program should be divided. So you do not write 150 lines as a main program it is too long it is too hard to figure out what is going on. So you break it up into pieces and the maximum size recommended for the main program or for any function is less than 20 lines. Why 20 lines? Because 20 lines you can sort of see at a glance. Anything bigger you sort of have to move your head around or you have to at least move your eyes around and if you are not seeing things at a glance then it is harder for you to sort of keep everything in your mind. The bigger the chunk that you have to keep in your mind the harder it is for you to work with it. So if you can break things into pieces which are about 20 lines then that is great. So you are more likely to understand that codefully and write it without errors. So again you may notice that I am really worried about making errors when I write programs and that is for two reasons. The first reason is that if I want to write a program that I give to someone else I want it to run correctly because I know that that person might be using that program for something really important and I would be failing in my duty if my program does not work correctly. The second reason I am worried about errors is that experience has taught me that I will make errors when I write programs and I should be prepared for that. I should be, I should take as many precautions as possible and I should not become proud that oh I am going to write this program without errors in the first shot. Sorry, it does not work like that for most people. So be a little humble, write the program, write it very carefully, write it by cultivating the style which prevents you from making errors as much as possible. But anyway be willing to test it and so yes I will be preoccupied about making errors and I will be suggesting strategies so that you do not make errors and breaking things into small pieces is one such strategy. Many experience programs even recommend smaller size for functions than 20 lines. So now how do you break code into small functions? So that is not quite what we are going to talk about here. That is something that we will consider when we write large programs, when we ask you to write programs at that point we will suggest how to break things into small pieces. The consideration this time is a little bit different. So this is the second consideration as to why we are talking about large programs. So as we said it is okay to put a 150 line program into a single file but not larger programs and again you may say that look so we are not going to write bigger than 150 line programs so why are we bothering? In fact yes the novice IDE of simple CPP requires you to only have a single file program. The full simple CPP IDE on the other hand does allow multiple files but we are not going to discuss it. So then why are we talking about all this? Here is the point when you write even your simple 15 20 line program you will be using code which is written by others. So effectively what you call your program is going to be a large program. Effectively you are cooperating with others who have written the simple CPP functions for you. So your program really contains all those simple CPP functions effectively. The math library functions such as square root, sine, cos whatever that you are using have been written by somebody else and they have to be a part of your program if your program is making calls to square root. So in some sense you should know how a large program works, how a program which exists across several files how do you put that together and that is really what this part is about. So you are collaborating with others without knowing it and you are using functions from many files. So you really should know how all that works. So to this extent even though you are not writing large programs what we discuss will be relevant to you. So what have we discussed in this last segment? We have said that the main program is a function and then we have articulated the need for splitting a program into many functions and we have articulated the need for splitting a program into many files. Next we are going to see how to split the programs into files and how your program will effectively use code written by others and finally we will see how you can use C++ without simple CPP. So we will take a break.