Hi,
Code that is a candidate for being inside a function:
Any code that might otherwise be repeated - we want to avoid that :+)
Anything in a compound statement. A compound statement is code between braces. So this might include anything inside a
while
or
for
loop. Ideally each
case
in a
switch
could call a function. Also, code in the body of an
if
or
else if
, or
else
statement.
This is an example of one general simple way to organise a program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
#include <iostream>
// other includes of STL headers
// other includes of your own headers
//function declarations
void GetInput(); // arguments up to you
void ValidateInput(); // arguments up to you
/*ReturnType*/ ProcessInput(); // arguments up to you
/*ReturnType*/ PrintOutput(); // arguments up to you
int main() {
// Reading the code in main tells an overview of what is happening
// Detail is in the function definitions
// must call the functions, otherwise nothing happens
GetInput(); // arguments up to you, must match the declaration above
ValidateInput(); // arguments up to you, must match the declaration above
ProcessInput(); // arguments up to you, must match the declaration above
PrintOutput(); // arguments up to you, must match the declaration above
} // end of main
//function definitions
void GetInput() { // arguments up to you, must match the declaration above
}
void ValidateInput() { // arguments up to you, must match the declaration above
}
/*ReturnType*/ ProcessInput() { // arguments up to you, must match the declaration above
// a function can call another function
// use this idea to break functions into smaller ideas
}
/*ReturnType*/ PrintOutput() { // arguments up to you, must match the declaration above
}
|
Other good things to do:
Write pseudo code. Start out with with general ideas, write them as comments. Go back, break down the problem into smaller ideas, and refine each item by putting more detail. Keep doing this until you are happy to convert the comments into code. Doing this helps identify what should be in loops, switches and functions.
Have meaningful variable names. Don't abbreviate too much, if at all. Write
Withdraw
rather than
wdraw
If the names of functions and variables are good, the code should be self documenting and tell a story of what is happening. Even if someone doesn't know the technical detail of what you are doing, they should still have a good idea of what is happening. Put comments to any web pages like wiki or other documentation if that is helpful.
Write your own documentation as comments, about what a function does.
Keep the scope of your variables as local as possible. Send variables to functions as arguments, don't have global's.
Make variables
const
wherever possible. Arguments / parameters are often
const
, because we shouldn't be able to change them inside the function.
Pass a variable by using a reference in the function parameter, to change the value of that variable in the outer scope. Does Turbo C++ 3.0 have references? - maybe not, well pointers then instead. Edit :
That might be more advanced
Write comments to say what the expected range of values for variables are - e.g. must be positive and less than 100 say. Edit :
This might give some clues about the type to use- consider unsigned int
instead of int
Write comments to say what the expected value of function arguments should be, for the function to work properly. This is called pre-conditions. Put this in as part of the function documentation, described above.
Write comments to say what the expected value of function return values should be, for the function to provide a valid answer. This is called post-conditions.
Some things not to do:
Global variables;
Goto;
Don't make functions too big - try for a limit of 40 lines say - or even less. This includes
main()
I converted some of your code into some function definitions:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
void ShowMainMenu () {
cout << "\nThank you for creating an account with us. To better serve you, please provide"
cout << " us the following information.";
cout << "\n\nAccount Type:";
cout << "\n [S]avings";
cout << "\n [C]hecking";
cout << "\n [Q]uit";
}
void GetUserMenuSelection(char& MenuSelection) {
cout << "\nEnter your choice:";
cin>> MenuSelection;
}
|
Hope this helps you out.