Select Page

Recursive Subprograms:

Recursive subprograms, also known as recursive functions or procedures, are functions or procedures that call themselves either directly or indirectly during their execution. In other words, a recursive subprogram is one that contains a call to itself within its own body. Recursion is a powerful programming technique used to solve problems that can be broken down into smaller, similar subproblems.

Key characteristics of recursive subprograms include:

  1. Base Case: Recursive subprograms typically include a base case, which is a condition that terminates the recursion. When the base case is met, the recursive calls stop, preventing infinite recursion.
  2. Recursive Case: Recursive subprograms also include one or more recursive cases, where the subprogram calls itself with modified arguments to solve a smaller instance of the original problem. This recursive call eventually leads to the base case.

Example of a recursive function in Python to calculate the factorial of a non-negative integer:

def factorial(n):

# Base case: factorial of 0 or 1 is 1

if n == 0 or n == 1:

return 1

# Recursive case: factorial of n is n * factorial(n-1)


return n * factorial(n-1)

# Example usage:
print(factorial(5)) # Output: 120

Static and Dynamic Scope:

Static scope (also known as lexical scope) and dynamic scope are two different approaches for resolving variable bindings in programming languages.

  1. Static Scope:
    • In static scope, the scope of a variable is determined based on its lexical context, i.e., where it is declared within the source code.
    • Variables are bound to their declarations based on the program’s structure, and their scope is fixed at compile time.
    • When a variable is referenced, the compiler or interpreter looks for its declaration in the nearest enclosing scope within the source code.
    • Static scoping is commonly used in most programming languages, including C, Java, Python, and JavaScript.

Example of static scope in Python:


x = 10

def foo():
print(x) # Accesses the global variable x

foo() # Output: 10

  1. Dynamic Scope:
    • In dynamic scope, the scope of a variable is determined based on the program’s execution flow, i.e., where it is called from during runtime.
    • Variables are bound to their most recent declaration in the execution stack, rather than based on the program’s structure.
    • When a variable is referenced, the interpreter looks for its most recent binding in the call stack, starting from the current execution context and moving upwards.
    • Dynamic scoping is less common and is mainly used in older programming languages or for specific purposes.

Example of dynamic scope (pseudo-code):


var x = 10;

function foo() {
print(x); // Accesses the global variable x

function bar() {
var x = 20;
foo(); // Output: 20 (Uses the local variable x)


In summary, static scope resolves variable bindings based on the program’s structure, while dynamic scope resolves them based on the program’s execution flow. Most modern programming languages use static scope due to its predictability and ease of understanding, while dynamic scope is used in specialized contexts or legacy systems.