Lesson 4: Functions Now that you have learned all about variables, loops, and if statements it is time to learn the next thing in programming: Functions. Obviously, you should have a good idea about what a function is, as you have used ones like cout before. However, this lesson will be more in detail about not only functions that are already made, but about making your own, or maybe I will continue this later... A good way to describe a function is to show its prototype. That means, what it should return, and what it should take as an argument. For example, the prototype of getch() is... int getch(void); The int means that it returns an integer, the void means that it does not take an argument. Now, you may know that getch returns a character! However, that does not mean it must return a character. The fact that the return type is an integer makes no difference, because the ASCII character set does not care what type of number it is, as long as it is a number...don't worry if you don't understand, it is not too important right now. What is important is that you understand prototypes. Another prototype is... int kbhit(void); It returns an integer, and it takes no value. Now that I hope you understand this then you will be able to use the help files much more easily, and I can go into more about functions. First, what functions are useful. There are many useful functions, and often they are hard to find. For Turbo C++ Lite some useful functions include, but are no limited to: cout<< iostream.h output cin>> iostream.h input int getch(void) conio.h get characters void clrscr(void) conio.h clear screen Okay, you might be thinking that this is nothing! Four measly functions, and you are right! If there were only a few functions then C/C++ would not be useful. However, a lot of programs do not need all that many functions. Of course, I suggest that you know ever function that you can, or at least the name. For this purpose, I will be posting an entire listing of ever function that I can find out of either help or books I have read. However, for now, these are probably the most useful functions. After all, if you can clear the screen, get input and output, and get keypresses, which are useful for stopping the program from immediately going back to the IDE you can do quite a bit! Believe me, there are a few specialized, but very useful funcitons, the thing is, you don't really need to use them all the time! If you have a problem with a function that you need, and I have not put up my list yet, then email me at lallain@concentric.net, and I will find you what you need! Anyway, after that long spiel on not needing a lot of functions, I am going to show you how to make your own functions! Wow, I can do that? Of course, otherwise C/C++ would not be useful! So, prepare to learn how to make functions. First let me give you an entire example program. Then we will look at it, and learn how to make our own programs. #include #include int mult(int x, int y); void main() { int x, y; cout<<"Input a number, and what number to multiply it by"; cin>>x>>y; cout< void function(void); void main() { function(); } void function(void) { cout<<"This is a useless and totally wasteful function"; } What is does is declare that there is going to be a function, by prototyping, and then at the bottom the function is defined, and it only does one thing...outputs "This is a useless and totally wasteful function" However, what if you wanted to do something that took 3 lines four hundred times in different places? Say, #include void function(void); void main() { LOTS OF CODE THAT NEEDS TO OUTPUT Line 1Line 2Line 3 } void function(void) { cout<<"Line 1"; cout<<"Line 2"; cout<<"Line 3"; } That, aside from the fact that it is a bad example, is where you would use it. When you need to call something a lot of times, but don't want to cut and paste. Functions are very useful, and I hope I explained them well enough for you to understand. Note: My homepage is http://www.cprogramming.com. My email is lallain@concentric.net. Please email me with comments and or suggestions. If you want to use this on your own site please email me and add a link to http://www.cprogramming.com. Thanks :)