> Can I call a function iside another function? Yes you can call a function inside another function. But before you can call a function you must have told the compiler what the call will look like. C++ uses a "prototype" to define what a call looks like. > Where should I put prototypes for my functions? It is wisest to list all prototypes just before the main program and after the #include statements. In fact #include statement includes a copy of a lot of prototypes of functions so that you can use them. > Where should I put function definitions? If you call a function then it must be defined. Function definitions must not be inside functions. It is wisest to put them at the end of the file that contains the program. > Where should I declare variables used in a function? Each function definition has its own variables and the best place for these is at the start of the function. > What does C Program with functions look like? Diagram of a file containing a complete program and functions: Comments about yourself Comments about the whole program #includes prototypes of all functions void main() { variables statements -- may call functions } Comments about function type function(arguments) { variables statements -- may call functions possible return } Comments about function type function(arguments) { variables statements -- may call functions possible return } Comments about function type function(arguments) { variables statements -- may call functions possible return } > Why is it that when I have global variables, my program works, but when I > change them to local variables within several different functions the program > outputs the wrong answers? Global variables are shared between the functions. Local variable belong to the functions and are not shared. In fact local variables are created each time you *call* the function... and destroyed when the function returns control. When a variable is created it has a random value. Here is an exercise... get a printout of the program with local variables and draw a box round each function body. Then mentally visualize the values trying to break thru the box walls and failing... Good programming relies on avoiding large numbers of global variables. It indicates that the programmer has not thought through the flow of data in and out of the functions. A global variable is uncontrolled and creates an invisible underground connection that can give rise to some nasty bugs in large programs. One way to make the flow explicit is used in the first part of our book (the CS201 part) - use the function headers, prototypes and calls to state what data is passed into functions (given) and which variables are shared with the function. In other words don't use globals to share data use arguments. The benefit is that the functions are more useful. For example... Suppose I want this simple algorithm read i and j add i and j giving k write k Below you can not see in the main program what is going on: //-------------------- int i,j,k; void read(); void add(); void write(); //------- void main() { read(); add(); write(); } //------- void read() { cin >>i >>j; } //------- void add() { k=i+j; } //------- void write() { cout << k;} //-------------------- Worse... if you the next program is read i and j add i and j giving k read l and m add l and m giving n add k and n giving m write m Then I have a big rewrite on hand... Below I redesign the program to separate the functions and make the connections explicit. //-------------------- void read(int &i, int &j); void add(int i, int j, int & k); void write(int k); //------- void main() { int i,j,k; read(i,j); add(i,j,k); write(k); } //------- void read(int&i, int&j) { cin >>i >>j; } //------- void add(int i, int j, int &k) { k=i+j; } //------- void write(int k) { cout << k;} //-------------------- And the rewrite only changes the main program void main(){ int i,j,k,l,m; read(i,j); add(i,j,k); read(l,m); add (l,m,n); add (k,n,m); write(m); } The functions can reused in many ways. As a rough rule of thumb - global variables can make you do more work than you need to. Is there an exception.... Yes. When the problem contains global information or resources: Outside the program, and existing from start to finish. The cin and cout channels are like this and are also global variables! Hope this helps.