C++ notes from Roseann Krane Functions: Structured modular programming is very important. By breaking programs into several smaller routines (functions), you can better isolate problems, write correct programs faster, and produce programs that are much easier to maintain. When you approach an application problem that needs to be programmed, it's best not to sit down at the keyboard and start typing. Instead, you should think about the program and what it is to do. Start with the overall program's goal and break it into several smaller tasks. Keep the overall goal of the program in mind and try to think of how the individual pieces fit together to accomplish this goal. The individual pieces of the program will be your functions. main() does not have to be the first function in a program, but usually main() is. Each function should do one primary task. Each function should act as a building block and perform only one distinct task in the program. Your program will be better organized with functions. The only thing that main() does is to control the other functions by showing, in one place, the order in which they are called. Each separate function does its thing and then returns to main(), which calls the next function until there are no more functions. The main() function then returns control to DOS. A good rule of thumb is that a function should not occupy more than one screen length. If it does, you are probably doing too much in the function and should break it into two or more functions. Each function generally should have the following properties: 1. Each function must have a name. 2. Each function name has one set of parentheses immediately following it. This helps C++ and you distinguish functions from variables. The parentheses may have something inside of them. 3. The body of each function, starting immediately after the closing parenthesis of the function name, must be enclosed by braces. Not every function requires a return statement as the last line, but including a return is recommended. It helps show your intent to return to the calling function at that point. Later you will see when the return is required, but for now, getting in the habit of including it will pay off. The primary function that controls function calls and their order is called a calling function. The functions controlled by the calling function are called the called functions. To call a function, you just type its name, including the parentheses, and then a semicolon. Remember that semicolons follow all executable statements in C++, and a function call (invocation) is an executable statement. The execution is the function's code being called. Any function can call any other function. You can tell the following statement is a function call: print_total(); because print_total is not a C++ command or library function name, it must be the name of a variable or user-written function. Only function names end with the parentheses, so print_total(); must be a function call or the start of a function's code. Because it ends with a semicolon, it must be a function call. Without the semicolon, it would have to be the start of a function definition. When you define a function -- function name and its subsequent code inside braces -- never follow the name with a semicolon. Only in main() where functions are called does a semicolon follow the function names. You cannot define a function within another one. All function code must be listed sequentially. A function's closing brace must appear before another function's code can be listed. You can execute a function more than once simply by calling it from more than one place in a program. If you put a function call in the body of a loop, the function will execute repeadedly until the loop finishes.