Programming Fundamentals/Scope
Overview
editThe scope of an identifier name binding – an association of a name to an entity, such as a variable – is the region of a computer program where the binding is valid: where the name can be used to refer to the entity. Such a region is referred to as a scope block. In other parts of the program, the name may refer to a different entity (it may have a different binding), or to nothing at all (it may be unbound).[1]
Discussion
editScope is the area of the program where an item (be it variable, constant, function, etc.) that has an identifier name is recognized. In our discussion, we will use a variable and the place within a program where the variable is defined determines its scope.
Global scope (and by extension global data storage) occurs when a variable is defined “outside of a function”. When compiling the program it creates the storage area for the variable within the program’s data area as part of the object code. The object code has a machine code piece, a data area, and linker resolution instructions. Because the variable has global scope it is available to all of the functions within your source code. It can even be made available to functions in other object modules that will be linked to your code; however, we will forgo that explanation now. A key wording change should be learned at this point. Although the variable has global scope, technically it is available only from the point of definition to the end of the program source code. That is why most variables with global scope are placed near the top of the source code before any functions. This way they are available to all of the functions.
Local scope (and by extension local data storage) occurs when a variable is defined “inside of a function”. When compiling, the compiler creates machine instructions that will direct the creation of storage locations on an area known as the stack, which is part of the computer’s memory. These memory locations exist until the function completes its task and returns to its calling function. In assembly language, we talk about items being pushed onto the stack and popped off the stack when the function terminates. Thus, the stack is a reusable area of memory being used by all functions and released as functions terminate. Although the variable has local scope, technically it is available only from the point of definition to the end of the function. The parameter passing of data items into a function establishes them as local variables. Additionally, any other variables or constants needed by the function usually occur near the top of the function definition so that they are available during the entire execution of the function’s code.
Scope is an important concept for modularization. Program control functions may use global scope for variables and constants placing them near the top of the program before any functions. Specific task functions use only local scope variables by passing data as needed into the function with parameter passing and creating local variables and constants as needed. Any information that needs to be communicated back to the calling function is again done via parameter passing. This closed communications model that passes all data into and out of a function creates an important predecessor concept for encapsulation which is used in object-oriented programming.
Key Terms
edit- data area
- A part of an object code file used for storage of data.
- global scope
- Data storage defined outside of a function.
- local scope
- Data storage defined inside of a function.
- scope
- The area of a source code file where an identifier name is recognized.
- stack
- A part of the computer’s memory used for storage of data.