0% found this document useful (0 votes)
15 views5 pages

Referencing Environment of a Subprogram

The referencing environment of a subprogram includes local variables, global variables, non-local variables, formal parameters, global constants, and imported entities, which dictate the accessible resources during execution. The behavior of these components is influenced by the scoping rules of the programming language, which can be static or dynamic. Java employs static scoping, ensuring a predictable referencing environment based on the program's structure.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views5 pages

Referencing Environment of a Subprogram

The referencing environment of a subprogram includes local variables, global variables, non-local variables, formal parameters, global constants, and imported entities, which dictate the accessible resources during execution. The behavior of these components is influenced by the scoping rules of the programming language, which can be static or dynamic. Java employs static scoping, ensuring a predictable referencing environment based on the program's structure.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

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
System.out.println(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() {


System.out.println(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() {
System.out.println(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) {
System.out.println(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() {


System.out.println(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 java.util.Scanner;

public void mySubprogram() {


Scanner scanner = new Scanner(System.in); // 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
System.out.println("Global Variable: " + globalVar); // Accessing global
System.out.println("Formal Parameter: " + param); // Accessing formal
parameter
System.out.println("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.

You might also like