Local Variables

We've seen local variables before - they're the ones defined inside the ``main'' function, inside a function you wrote, or in the formal parameter list. Take a look at just about any of our previous programs to see some local variables. This subsection is just here to distinguish local variables from global variables which are in the following subsection.


Global Variables

So far, all of the variable declarations we've seen so far, have been declared inside a function (whether it be the ``main'' function or some other one that we've written). But we can declare global variables that are ``accessable'' by everything by declaring them outside of the main function. Consider the following :

// file : global.C
#include <iostream.h>

int max;

void foo()
{
  if (max == 5)
    cout << "Yup...\n";
}

int main()
{
  max = 5;
  cout << "max is " << max << endl;
  foo();

  return 0;
}

Notice that both the ``main'' function and the function ``foo'' have access to the global variable called ``max''.