Referencing Environment of a Subprogram
The referencing environment of a subprogram is the collection of variables,
constants, functions, and other program entities that are visible and accessible
within the subprogram. These entities can be defined either within the subprogram
itself or in the broader scope (e.g., global or enclosing scopes).
Components of the Referencing Environment
1. Local Variables
○ These are variables declared within the subprogram and are only
accessible within the subprogram's scope.
○ They are created when the subprogram is called and destroyed when it
exits.
Example:
public void mySubprogram() {
int localVar = 10; // Local to mySubprogram
[Link](localVar);
}
○
2. Global Variables
○ Variables declared outside any subprogram and accessible within the
subprogram.
○ Their lifetime spans the entire program's execution.
Example:
int globalVar = 50;
public void mySubprogram() {
[Link](globalVar); // Accessing global variable
}
○
3. Non-local Variables
○ Variables declared in an enclosing scope but not local to the
subprogram.
○ These can be variables in a parent function or module.
Example:
public class Outer {
int outerVar = 30;
public void outerFunction() {
class Inner {
public void innerFunction() {
[Link](outerVar); // Accessing non-local variable
}
}
new Inner().innerFunction();
}
}
○
4. Formal Parameters
○ These are variables used to pass data into the subprogram.
○ They act as local variables initialized with the arguments passed when
the subprogram is called.
Example:
public void mySubprogram(int param) {
[Link](param); // Accessing formal parameter
}
○
5. Global Constants
○ Constants that are accessible in the subprogram and declared at a
global level.
Example:
public static final int CONSTANT = 100;
public void mySubprogram() {
[Link](CONSTANT); // Accessing global constant
}
○
6. Imported Variables or Functions
○ Entities imported from external modules or libraries, making them
available in the referencing environment.
Example:
import [Link];
public void mySubprogram() {
Scanner scanner = new Scanner([Link]); // Using imported class
}
Static vs. Dynamic Referencing Environment
1. Static Referencing Environment:
○ In static scoping (lexical scoping), the referencing environment is
determined at compile time based on the program's structure.
○ Languages like Java, C, and Python use static scoping.
2. Dynamic Referencing Environment:
○ In dynamic scoping, the referencing environment is determined at
runtime, based on the call stack and calling sequence of subprograms.
○ Dynamic scoping is less common and found in older languages or
scripting contexts.
Example to Illustrate Components in Java
public class ReferencingEnvironment {
static int globalVar = 100; // Global variable
public static void main(String[] args) {
int localVar = 10; // Local variable
mySubprogram(localVar);
}
public static void mySubprogram(int param) { // Formal parameter
int localToSubprogram = 20; // Local variable
[Link]("Global Variable: " + globalVar); // Accessing global
[Link]("Formal Parameter: " + param); // Accessing formal
parameter
[Link]("Local Variable: " + localToSubprogram); // Accessing
local variable
}
}
Output:
Global Variable: 100
Formal Parameter: 10
Local Variable: 20
Summary
The referencing environment of a subprogram consists of:
1. Local Variables
2. Global Variables
3. Non-local Variables
4. Formal Parameters
5. Global Constants
6. Imported Entities
These components determine what resources are accessible during the execution of
a subprogram, and their behavior depends on the scoping rules of the language
(static or dynamic). Java primarily uses static scoping, making the referencing
environment predictable and tied to the code's structure.