Local and global variables. Area of ​​visibility.

c++, local and global variables, the scope of variables, c ++ beginnersFor 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:

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:

Variable k It called local, 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:

c++, local and global variables, the scope of variables, c ++ beginners

but, one must also take into account the, that the scope is extended to the indoor units. For example, consider this code:

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:

It describes the scope block statement, limit {}, That is. first cout displays 2, because the defined block of statements, in which it works cout. And in the same block is defined local variable named and, from it he will value above all.

c++, local and global variables, the scope of variables, c ++ beginners

And already outside {} operator cout displays 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 name i. 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 to view a video tutorial on the scope. Author video tutorial: Markov Denis

Newsletter of programming:

Local and global variables. Area of ​​visibility.
4 (80%) 1 vote

Leave a Reply

Your email address will not be published. Required fields are marked *