The concept of global and local data storage is usually tied to the concept of scope. Scope 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 variable 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 to modularization. Program Control functions usually use global scope for variables and constants placing them near the top of the program before any functions. Specific Taskfunctions 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.
- Global Scope
- Data storage defined outside of a function.
- Local Scope
- Data storage defined inside of a function.
- Data Area
- A part of an object code file used for storage of data.
- A part of the computer's memory used for storage of data.
- The area of a source code file where an identifier name is recognized.