C++ Tutorial/Function/function scope variables
Версия от 14:21, 25 мая 2010; (обсуждение)
A local variable is initialized each time its block is entered
#include <iostream>
using namespace std;
void f();
int main()
{
for(int i=0; i < 3; i++) f();
return 0;
}
// num is initialized each time f() is called.
void f()
{
int num = 99;
cout << num << "\n";
num++; // this has no lasting effect
}
99 99 99
Function level variable
#include <iostream>
using namespace std;
void f1();
int main()
{
int val = 10;
cout << "val in main(): " << val << "\n";
f1();
cout << "val in main(): " << val << "\n";
return 0;
}
void f1()
{
int val = 88;
cout << "val in f1(): " << val << "\n";
}
val in main(): 10 val in f1(): 88 val in main(): 10