C++/Function/Variable Scope
Содержание
Effect of scope on automatic variables
#include <iostream>
using namespace std;
int main()
{
int count1 = 10;
int count3 = 50;
cout << "Value of outer count1 = " << count1 << endl;
{
int count1 = 20;
int count2 = 30;
cout << "Value of inner count1 = " << count1 << endl;
count1 += 3;
count3 += count2;
}
cout << "Value of outer count1 = " << count1
<< "Value of outer count3 = " << count3;
return 0;
}
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;
}
global variables
#include <iostream>
using namespace std;
int glob = 10;
void access_global();
void hide_global();
void change_global();
int main()
{
cout << "In main() glob is: " << glob << "\n\n";
access_global();
hide_global();
cout << "In main() glob is: " << glob << "\n\n";
change_global();
cout << "In main() glob is: " << glob << "\n\n";
return 0;
}
void access_global()
{
cout << "In access_global() glob is: " << glob << "\n\n";
}
void hide_global()
{
int glob = 0; // hide global variable glob
cout << "In hide_global() glob is: " << glob << "\n\n";
}
void change_global()
{
glob = -10; // change global variable glob
cout << "In change_global() glob is: " << glob << "\n\n";
}
The Scope Resolution Operator
#include <iostream>
using namespace std;
int count1 = 100;
int main()
{
int count1 = 10;
int count3 = 50;
cout << "Value of outer count1 = " << count1 << endl;
cout << "Value of global count1 = " << ::count1 << endl;
{
int count1 = 20;
int count2 = 30;
cout << "Value of inner count1 = " << count1 << endl;
cout << "Value of global count1 = " << ::count1 << endl;
count1 += 3;
count3 += count2;
}
cout << "Value of outer count1 = " << count1 << endl
<< "Value of outer count3 = " << count3 << endl;
return 0;
}
Variable Scope Example
#include <iostream>
using namespace std;
int subtract (int a, int b);
int global = 5;
int main(void)
{
int a, b;
a = 5;
b = 3;
cout << "The value of main"s a is: " << a << endl
<< "The value of main"s b is: " << b << endl
<< "The value of global is: " << global << endl;
global = 2 + subtract(a,b);
cout << "The value of main"s a now is: " << a << endl
<< "The value of global now is: " << global << endl;
return 0;
}
int subtract(int a, int b)
{
cout << "The value of subtract"s a is: " << a << endl
<< "The value of subtract"s b is: " << b << endl;
a = a - b + global;
return a;
}
Variables: Global, Local variable
#include <iostream>
using namespace std;
int gVar1;
int gVar2 = 2;
int main()
{
char ch("A");
cout << "Value of gVar1: " << gVar1 << endl;
cout << "Value of gVar2: " << gVar2 << endl;
cout << "Character in ch: " << ch << endl;
int sum, number = 3; // Local variables with
// and without initialization
sum = number + 5;
cout << "Value of sum: " << sum << endl;
return 0;
}