For the understanding of localization variables probably start with an allegory. What “Locality”? This limitation of capacity space. Variable scope – this is the piece of code (function, loop, namespaces), in which the variable is declared (spelled). Outside of this area – the compiler does not see it (it is not available).
So it turns out – Variables such as described within functions, They can not be used abroad. As an example, consider the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #include <iostream> int i = 2; // глобальная переменная (видна в любом участке кода программы) int sum() { int k = 2; // локальная переменная (видна только внутри функции sum()) return i + k; } int main() { std::cout << i << std::endl << sum() << std::endl; } |
Let's look at an example of a closer. So we have a variable i, described is any function in the program. Its scope and actions – the entire program is not limited to. And once she “legitimate” the entire program, in all its functions and operators blocks, prisoners {}, then it is called Global variable.
As we have seen in the example of the function sum(), that something does. It is described in the second variable – k. She already has a specific “registration” – function. Furthermore, this variable can not be used outside the function. That is. if function main() finish is also the conclusion of the k the screen, compiler starts “swear”. It it just simply does not see outside the function sum(). Check out:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #include <iostream> int i = 2; // глобальная переменная (видна в любом участке кода) int sum() { int k = 2; // локальная переменная (видна только внутри функции sum()) return i + k; } int main() { std::cout << i << std::endl << sum() << std::endl; std::cout << k << std::endl; // попытка обратиться к переменной k } |
Variable k It calledlocal, and its area of visibility defined opening and closing braces function sum() – {…}. Then they do not have it running, therefore it can not be used outside this function. Compile:
but, one must also take into account the, that the scope is extended to the indoor units. For example, consider this code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include <iostream> int i = 2; int sum() { int k = 2; for (int i = 0; i < 10; i++) // эта i - локальная, объявленная в теле цикла k += 1; return i + k; // эта i - глобальная, объявленная вне функций } int main() { std::cout << i << std::endl << sum() << std::endl; } |
It manifested the following scope:
- global – i = 2 hers;
- Local in the function – variable k;
- Local relative for loop() – second i.
Despite, that for has its own scope, himself for member function sum(). And it means, and everything in it is, It is subject to the local scope of the function. That is. all variables, function defined in the same way and in the actual body for, which allows the operator to work k += 1 (he is k = k+1 or k ).
More interesting is the case of a variable i, described in for. Despite the name, identical with the global variable as described above, that's another variable.
As soon as their cycle worked, variable i, described in it, loses its power, or in other words – “freed up” from duties. I'm not kidding – just that the memory manager arrives. When a variable is described, Manager reserves under her memory, and when its scope ends – This frees the memory. Memory manager at marks, that this memory location is not owned by anyone, and it is possible to give a recording of other data.
To fix another light primerchik:
1 2 3 4 5 6 7 8 9 10 11 12 13 | #include <iostream> int main() { int a = 0; { int a = 2; std::cout << a << std::endl; } std::cout << a << std::endl; } |
It describes the scope block statement, limit{}, That is. first cout displays 2, because the defined block of statements, in which it workscout. And in the same block is defined local variable named and, from it he will value above all.
And already outside {} operator coutdisplays 0, because there is not the same for him and, that equals 2. Its scope is the same as the other and, which equals 0, and picked up a nested block.
Generally remember – in real life you should never give the same names of variables (except, that is classically used for loop counters. For example i, j, k. If you 10 loops in the code and they are not invested, then for each of them can declare and define the counter with a namei. This is normal). In other cases,, always give unique names to the variables, if you do not want, to you later “recalled” programmers, which will be dealt with in your code.
We also offer watch the video-lesson on the scope.