Section 3 - Defining Methods
5A.3.1 Defining the First Method
To keep things easy to read, I'll just present the definition of the first method, stateInstructions() so we can see where it goes in the class. The method definition comes right after the main() method, and still inside the one and only class (called Sample this week):
#include <iostream> #include <string> #include <cmath> using namespace std; // method prototypes void stateInstructions(); void getInputAndComputeMonthlyPayment(); void sayGoodbye(); int main() { stateInstructions(); getInputAndComputeMonthlyPayment(); sayGoodbye(); } // gives an overview to user void stateInstructions() { string instructions; instructions = "\nThe following program will calculate the \n" "monthly payment required for a loan of D dollars \n" "over a period of Y years at an annual \n" "interest rate of R%.\n"; cout << instructions; } // does all the work - gets input, computes and reports answer void getInputAndComputeMonthlyPayment() { // Body of this method omitted for now } // sign off void sayGoodbye() { // Body of this method omitted for now }
First we see a section called method prototypes.
void stateInstructions()
This is where we declare our functions (methods), which roughly means we give a short description of each function that we intend to design and use. Then we proceed to define main() followed by the definitions of our three new functions.
The definition of the stateInstructions() method looks a lot like the beginning of the definition of our older main(). Also, the definition of each of the methods (including the ones we did not fully list yet) apparently begins with a method header that contains the word void followed by the user-defined method name, and then empty parentheses, as in:
void stateInstructions()
The word void means that the method does not return a value directly to the main().
Method definitions can appear in any order inside the source file.
A method is called (or invoked or used) when its name appears in main(). That is, the program begins at the first line in main() and only gets into a method if there is a line in main() that has the method's name on it. That means that the method stateInstructions() is actually executed only if there is a line in main() that has the word stateInstructions() in it.
When a method is called, the program execution switches to the top of the method definition and continues until the method ends, or a return statement is reached. When either of these things happens, the control passes back to main(), to the next statement after the method call.
In main() you see three method calls. This breaks main() into three modular pieces. We could give different teams responsibility for designing and debugging different methods, but in such a small program, this is not needed.
When you look at main() now, it is easy to see where things happen. If there is a problem with the opening remarks, or the arithmetic, we know which method to look into to fix it.
Reminder About String Concatenation
Recall that I told you we could break string literals into shorter sub-strings and place each sub-string onto its own line. The simple syntax was as you see above: we close the quote at the end of the first line, then start a new quote on the next line. This has the effect of concatenating the strings for us.
An important observation is that breaking up string literals like this has no effect on the output of the program. Unless we add newline chars ('\n') in the string, the output will still have the entire string appear on a single line. That's why we used '\n' in the literals.