Introduction to Programming/Scope
The Motivations For Scope
editImagine that you have a really nice collection of tools—really nice. Now imagine that you decide to lend your tools to any neighbor who asks. Unless you have exceptional neighbors, you will probably find it necessary to modify this policy eventually because some of those tools will end up broken and others will disappear forever without a trace.
In the early days of computing, computer memories were small, the amount of data being processed was small and the size of the programs were small. In those days, all of the data was kept in one place and could be accessed by any part of the program. As computers and programs got larger and more complex, it was found that it became far too easy for one part of a program to mess up the data needed by another part of the program. Furthermore, it became difficult to determine which parts of the program were using the data correctly and which were not.
Features were added to programming languages so that parts of a program could control the access to their data.
Scope
editScope refers to the visibility of variables and methods in one part of a program to another part of that program. The importance and meaning of scope varies between languages, but there are two general scope concepts many languages have in common: local and global scope.
Local Scope
editVariables or methods that have local scope (and for object-oriented programming, this includes instances of classes) are accessible only in the current block of code. There is a sort of "nested sets" model to the notion of scope. Beginning with global objects (see below), each contained block may access objects defined in a containing block; that is to say, in a code fragment in which a variable is defined and the program enters some kind of loop block, that loop sub-block does have access to that variable defined outside of the block, and it may, additionally, override it temporarily by declaring a variable of the same name in its own local scope (the scope of that loop block). See the example below. Local scope essentially refers to variables that are limited, as far as access is concerned, to the most current block of code, and not necessarily to any "outer" blocks that might also be counted in the nested model.
Global Scope
editCertain variables and methods can be said to have global scope. This term encompasses a very qualified situation: all of the variables defined at the very beginning of a program are available to the entire program. Likewise, in a function, the variables declared at the beginning are available to all the remaining code in that function.
Scope Resolution
editMost programming languages allow the programmer to "step outside" the current local scope and bring in variables or methods "from the outside" so to speak. In C++, this is accomplished using the "scope resolution operator" which is the double-colon: :: By using this operator, the function or program can obtain access to data members located in another "branch" of the program. If the structure of containment of variables in a program is thought of as a family tree, this means that a child requests access to his cousins through, say, the grandparents. e.g. "My mom's sister's daughter's dolls" is an example of stepping through the tree, and such a reference would use the scope resolution operator, although it is rare to see more than one level referenced in a statement containing the ::.
Duplicate Names
editVariables used in a program, especially one involving mathematics, often are designed to solve a problem in which the same letters are ordinarily used on paper, but even then represent different values. Think of the x or a and b. A programmer must pay very careful attention to this fact and not "re-use" variable names, because to the computer, they are all the same. With one exception. It is possible to override a local variable that is defined slightly above the current block, by declaring another variable of the same name and possibly the same type, inside of this current block. This new variable with more local scope than the first can be used within the current block, but because the outer variable has been temporarily overridden, the new variable's value will hide whatever the outer variable's value is. Here is a C++ example:
int a=25; int b=30; { ''//at this point, a=25, b=30'' a*=2; ''//a=50, b=30'' b /= 2; ''//a=50,b=15'' int a = b*b; ''//a=225,b=15'' } ''//a = 50, b = 15
As you can see, the two variables were manipulated within that inner block, but then a second version of a was created with a new value, that was lost once we passed out of the block. The other changes were preserved, however.
Even with an understanding of variable scope, it is always good programming practice to declare variables at the beginning of their respective block, actually as high up as is practical in the hierarchical structure of the program. This is so that they can be easily referenced and there is no ambiguity as to whether a variable has been defined at any given point in the program.