Section 1 - Globals

5B.1.1  Global Variables

Thus far we left an item unresolved - how one transfers data between the client and the functions. So far, we have only one way: the functional return value. And we can only get a single value that way, so that is a limited modality.

A second method for exchanging data among different functions is through the use of global variables or globals.

Let me begin by saying globals are bad, and should be avoided. (However, they aren't as bad as, say, the goto statement which is so bad most instructors don't even mention it). It is enough to know that you should avoid globals most of the time. However there are those rare occasions when they are needed.

Also, when learning  C++, globals are handy because they give the student a way to deal with the sharing of information between functions in an elementary way, before learning about such things as arguments and pointers.

So we cover them here, and you can use them until you learn safer ways to pass information between the client and the function.

We saw that local variables are defined inside functions, at the top, below the function header, but above the executable statements. These locals were not shared.

Globals are defined outside, and usually above any of the functions. They are known to all functions in the file (if there are multiple files in a project there are other rules we won't discuss now).

5B.1.2 Using Global Variables to Share Data

Now we think back to our mortgage calculator and ask what was one of the remaining weaknesses in this program?  The answer is that one of the methods was too long and did too much.  We were getting data from the user and also computing the monthly payment in the same method.  This is bad not only because it gives one method too much responsibility, but more importantly, because we are mixing I/O with calculations.  We can now fix this.

So we will want one method to get the input from the user, and a second method to do the calculations.  We will break up getInputAndComputeMonthlyPayment() into two smaller methods:

Even the names are more palatable!

decorative coins

Somehow we have to transfer the information that we receive from getInput() into the method computeMonthlyPayment(). We will use global variables to do this. But which variables are needed by both method and which can we declare as local in each?  After all, we do not want to declare variables as global variables if they are only needed by one method.

Let me repeat that last statement since it is so important that points will come off if you don't obey it. If only one method uses a variable you do not want to declare it as a global variable.  You should instead, declare it to be a local variable (as we've been doing up to this point) to the one method that uses it.

So which variables of the mortgage calculator should be declared as global data?

These are the variables that are needed by both methods:

  1. dblPrincipal
  2. dblRate
  3. dblYears

These are the variables that the user gives us (or will give us) in getInput() and they are needed to compute the monthly payment.  So we declare them as global data:

// global variables shared by more than one method
double dblPrincipal, dblRate, dblYears;

// main method
void main ()
{
// content of main() omitted
}
// gives an overview to user

void stateInstructions()
{
// content of method omitted
}

// gets input, stores in static class variables
void getInput()
{
// content of method omitted
}

// etc ...

As you can see, we have declared these three variables inside the class and above all of the methods.  Now, any of the methods in the file can use these variables without re-declaring them (in fact re-declaring them inside a method would be a major error).

After you understand the concept of data sharing and the syntax for declaring global variables, go on to the next section where we apply it to the mortgage calculator.