previous | start | next

## Variable Scope and Global Variables (Variable Scope)

- You can have variables with the same name in different functions.
- The part within a program in which a variable is visible is known as the scope of the variable.
- The scope of a variable extends from its definition to the end of the block in which it was defined.
- Example:

  >     double future_value(double initial_balance, double p, int n)
  >     {
  >        double r = intial_balance * pow(1 + p / 100, n);
  >        return r;
  >     }

  >     int main()
  >     {
  >        cout << "Please enter the interest rate in percent: ";
  >        double r;
  >        cin >> r;

  >        double balance = future_value(10000, r, 10);
  >        cout << "After 10 years the balance is" << balance
  >           << "\n";

  >        return 0;
  >     }

previous | start | next