Section 2 - Methods (Functions)
5A.2.1 Rationale
We have been using methods by invoking them (getline(), isalpha(), etc.) So far, you could only invoke methods that I told you existed. This week you get to create your own methods, and you get to decide how they are called.
Methods, also known as functions in C++, are important for many reasons, but the one-word justification for them is modularity. It would be nearly impossible to write and maintain medium-sized or large programs without breaking them down into palatable and debug-able parts. We will soon see that these parts consist of classes (which we have briefly touched on), and the most important aspects of the classes which are the methods. While we won't delve into classes until next week, we can start learning methods immediately.
5A.2.2 Introducing Methods
A method is a sub-program that is dedicated to performing a relatively small task. By relatively small, I mean anything that is easily describable in a sentence or two, doesn't take too many statements to execute (subjective), and is easily tested.
It may also be a task that is called repeatedly by your program, sometimes with different data being sent to the method for processing.
We will learn methods gradually over the next two weeks. The first example will not obey all of the above principles. For one thing one of the methods will perform a large task and take a lot of statements. This will be fixed in a subsequent version of the program when we learn how to break the task down better.
5A.2.3 A Mortgage Calculator
We're going to write our own mortgage calculator - one that computes monthly payments based on interest, term of loan and principal amount.
Here is a sample of what we are after:
We will use the following formula to compute the payment. In this formula
- L is the amount of the loan
- c is the monthly interest rate (we'll have to convert from annual to monthly)
- n is the number of months (we'll have to convert from years to months)
- P is the monthly payment we are seeking
Here is the formula. Please don't panic. You don't have to memorize it. You will learn how it gets turned into a C++ formula. Ready? Hold your breath:
or, written as a single line:
In the following pages we will write increasingly smarter versions of the program that uses methods to accomplish all of this.
5A.2.4 Simplifying main()
One of the main reasons for writing and using methods is that it makes our programs easier to read and understand. Let's start by seeing how our main() method (the only method we have written up until now) will look once we have written some other methods to make it look clean, short and easy to understand.
int main() { stateInstructions(); getInputAndComputeMonthlyPayment(); sayGoodbye(); }
Before continuing, lets compare this main() to the screen shots above.
We see a method call to stateInstructions(). This method is pretty clear, but can you see which of the screen output (in my screen capture image) this method controls? If you answered only the first paragraph, you are correct. It places the overall introduction on the screen.
Next we invoke a method called getInputAndComputeMonthlyPayment(). Which paragraphs do you think that one controls? It will place on the screen the four middle paragraphs: the three inputs and the one result that reports the monthly payment. This is the method that I said will be too large and do too much in this first incarnation. That's okay, though, because right now we won't have the machinery to break it down. We will do so, in future revisions.
Finally, there is the sayGoodbye() method call. This one obviously presents the final paragraph on the screen, thanking the user and signing off.
Our first task will be to learn how to define (write) these three custom-made methods. This will be the first time that we have ever defined our own methods that we can use (i.e., invoke) inside main().