 Welcome back, in the previous segment we discussed that the main program is a function and we motivated the need for splitting a program into functions as well as into several files. In the next segment we are going to, in the rest of this lecture sequence actually, we are going to see how to actually split programs into files and that is what is going to happen in this segment. And then in subsequent segments we will see things like how your program uses code written by others and features that C++ provides for that and also finally we will see how you can use C++ without simple CPP. So how do you split program, split a program into many files? So a program may contain several functions and all of them need not be placed in the same file. So if a code in file capital F calls a function little f then the function f must be declared inside f actually before any call to it. Now a function definition that we have been talking about is a declaration but that is not the only way to declare a function. So the definition could be in another file and so long as there is a declaration in the file capital F which contains calls to this function that is enough. So every function must be defined in just one of the files that are used for a program but it must be declared in all the files that make a call to that function. So what is a function declaration? Well it is essentially the definition without the body. So declaration of the GCD function looks like in GCD int nn. So after that you could have given the body but that is being omitted in a declaration. Also acceptable is n GCD int nt. Now you may notice that this is really like declaration of a variable. So I might have written int x so it looks like a declaration of a variable but it is the parenthesis are saying that it is not a variable but because of those parenthesis following the GCD the compiler can tell that oh this is a function that we are talking about. So basically that is what a declaration tells the compiler. It tells that look GCD is a function and should the name GCD appears, appear later on in this file, think of it as a function which takes two arguments. And the arguments had better be of the specified type. So this way the compiler checks that subsequent uses of the name GCD adhere to what you have said about GCD. Again the general strategy over here is to say what GCD is and then check whether you are using GCD in the manner that you have just described. This is also because we are worried about making mistakes. So the compiler tries to help you out and tries to see if you are possibly making a mistake and therefore it needs to know what every name is whether it is a function, it is a variable and therefore the declaration has to be given preceding the use of that name. Now if a file contains a call to a function but does not contain its definition the compiler will assume that it will appear elsewhere. In fact that is the whole point of putting in a declaration. So putting in that in GCD int n, int n without the body. But the compiler will not be able to compile it as a main program. In fact unless the main program is there it will not be able to compile but furthermore if some function body is missing again it will not be able to compile. What it can compile your file into is what is called an object module and we will see that. To get an executable program all the object modules containing the called function must be linked together only then you can produce an executable program. We said this long ago when we were talking about how the hardware works but now you will see a little bit a few additional details. The function declaration that we have talked about so this line this, this, this portion this or this is called a declaration but it is also called the signature of the function or the prototype of the function. So some examples. So here is an example of a program which is split over multiple files. So you can have a program consisting of a function in GCD with the body also given and perhaps you can put it in the file gcd.cpp. You can also have a second file lcm.cpp, a third file main.cpp. So what is in these files? So the green portions are function definitions and the red portions are the function declarations and each file you will see has a declaration, declarations of the called function. So in this file there is a call to GCD and the definition the function is not defined over here but the function is declared in this file. So this is the declaration. So the compiler can compile this file at least partially into an object module. Then in this file main.cpp lcm is being called and it is not defined over here but there is a declaration saying that oh it contains it has two arguments and in fact compiler can check that indeed two arguments of type integers have been given. Now if you have these files and you want to compile and link them together you would issue the command s++ main.cpp lcm.cpp gc.cpp if you are operating under Unix and these files were in your current directory. If you are operating, if you are using the full IDE then you would have defined something called a project in which these files would have to be present. But the contents of the files would have to be exactly these that if a file uses a function makes a call to a function and if the function is not defined there then there has to be a declaration of it above the use or the call to that function. If you want to compile each file separately this is what you do you write s++ but you add this compiler option minus c which essentially says compile only or do not link lcm.cpp. So this produces lcm.o which is an object module which you can think of as something which is partially compiled. And if you have all these object modules then you can compile them together by writing s++ again s++ does everything as you can see the compiler does everything. You can write main.o lcm.o gcd.o so this will actually produce your executable. Now what you typically do is not quite this. What you typically do is that you write a program say in a file called pgm.cpp on Unix you compile it but the s++ command itself supplies all the additional modules that you want. So if you are using square root that module is supplied. If you are using simple cpp the modules required for simple cpp are supplied. So this compilation is really quite similar to this compilation or even this compilation even though you are using only a single file. So this s++ is compiling your program pgm.cpp but you are also linking it to object modules which are developed by others. So where are they? They are in some places that this s++ knows and some of them might be present as libraries. So it is not only object modules that you can compile together. You can also compile together libraries. So the libraries might also get listed over here. So we are going to omit that detail because s++ places those libraries for you but they are sort of like object modules. They also contain code for functions. So for all this to work correctly your program must have the right declaration. So if this is your program then it had better have a declaration of the function that you are going to use. Now you may say that look I use square root, I used write, I used turtle sim, I used init canvas but I did not put declarations to all those in the program that I wrote. So there is a different mechanism for putting in the declarations which I am now going to tell you about. So this mechanism is called header files. So it is tedious to remember what declarations to include in each file. So what do we do instead? We put all the declarations into a file called the header file and include the header file into every file that we use. So here for example is the header file gcdlcm.h, I just made up a name. So this file contains ngcd intlcm so it contains the declarations of gcd and lcm. And now the file gcd.cpp does not actually contain the declaration but it says include this file. So all those declarations would go in. lcm similarly just includes this file and main also includes that file. So basically I do not, I am not worrying about oh include the declaration for this function, I am not going to worry about this function, that function. I am just going to say look whatever functions I need those and maybe more than those are present in this header file. And I am just going to include my header file and that will automatically bring in these lines. So the directive include file name just copies the contents of that file into your file. So it gets replaced by the contents of the name file. And it is acceptable if we declare functions that do not get used. So for example in this if we include gcd lcm.h this file itself directly does not need the declaration for gcd but it is okay to have it. It just says that gcd is a function, whether I am using it or not is not important. It is acceptable if we have both a declaration and then the definition. So that is going to happen over here. So this include is going to be replaced by ngcd int int which is just a declaration but it is also going to contain a definition but that is okay. We can have a declaration and a definition. So long as they are consistent of course. What we cannot have are two definitions. So two pieces of code cannot be given. We will also have noted that there is another include over here include simple cpp. Now it is in this braces but anyway that simple cpp file is included it is picked up from some specific places and that file contains the declarations of all things like turtle sim right left also things so all those things and that is why you have not been needing to actually include put in declarations for all the functions that you have been using so far. And that file simple cpp also includes an include file itself called cmath which contains the declarations of square root sign and all of that. So the include files have been putting in all the declarations that you really needed. So a little bit more on header files. Header files customarily have the suffix .h which is what we just used. They could also be .hpp or sometimes they do not have any suffix that is also allowed. If the header file is mentioned in quotes it is picked up from the current directory if it is mentioned in angle braces then it is picked up from some standard place. For example simple cpp is present in some standard place which the compiler knows about. So if you say include simple cpp s++ will pick it up from that standard place. So what have we discussed? We have discussed how to split a program over many files and effectively how do you assemble a program out of functions in many files and I guess this is really what is most relevant for you because you are not going to split a program but you are going to use functions which are there in several programs. So you want to know how to use those functions. Then we have discussed what are function declarations and definitions and we also talked about header files. We are going to talk about namespaces which is a c++ feature which helps in writing programs across multiple files. So we will take a break.