Function with global value : Variable Scope « Function « C++






Function with global value

  
#include <iostream>
#include <cmath>

using namespace std;

double balance;

/** 
   Accumulates interest in the global variable balance
   @param p the interest rate in percent
   @param n the number of periods the investment is held
*/
void future_value(double p, int n)
{  
   balance = balance * pow(1 + p / 100, n); 
}

int main()
{  
   balance = 10000;
   future_value(5, 10);
   cout << "After ten years, the balance is "
      << balance << "\n";
   return 0;
}
  
    
  








Related examples in the same category

1.Variables: Global, Local variableVariables: Global, Local variable
2.Variable Scope ExampleVariable Scope Example
3.The Scope Resolution Operator
4.Effect of scope on automatic variables
5.global variables