int age;
void Function1(int theAge); ... int main() { int age; ... Function1(age) } ...
void Function2(int & newAge); ... int main() { int age; ... Function2(age) } ...
void AFunction() { int localVar; // lifetime of localVar begins here. // code while() { // localVar is still in lifetime } AnotherFunction(); // while AnotherFunction is executing localVar is still in it's lifetime. return; // lifetime of localVar ends here }
void AFunction() { int localVar; // scope of localVar begins here. // code while() { // localVar is still in scope } AnotherFunction(); // while AnotherFunction is executing localVar is not in it scope. return; // scope of localVar ends here }
void Function(int aParam){ int aVar // aParam and aVar are in scope here. while() { int i; // this goes against local convention. // aParam, aVar, and i are in scope here. } // i is no longer in lifetime or scope. }
for(int i = 0; condition; increment) { body }
i
is local to the loop.
void Function(int aParam){ int i{4} ; while() { int i{22}; // which i is used here? i = 7; } // which i is used here? cout << i }