15 lines
1.1 KiB
HTML
15 lines
1.1 KiB
HTML
<p>
|
|
A scope defines the area in your code, where certain names are visible. This is useful, because at some point you would run out
|
|
of meaning full names. The most simple example of the usefulness of scopes are for-loops.
|
|
People like to call the loop-counter i and j or x and y or n and m. If you don't have scopes, you could only use the name i ones in your entire program.
|
|
</p>
|
|
<p>
|
|
There are different scope levels. There is the global scope, which is for all global variables of your program, then there is the scope of the compilation unit.
|
|
For all intents and purposes is basically the same. When you start to write bigger programs you may break your codes in different compilation units. But for
|
|
now you don't have to worry. Then there is block scope. Each block has its one name-space. So if you declare a function, if-statement, some kind of loop or even
|
|
just a block, it will have its own scope.
|
|
</p>
|
|
<p>
|
|
You can declare variables with the same name as one in an higher scope. This will overwrite the name till the end of the scope. This is called shadowing
|
|
<p>
|