• Home
  • What is lexical scope and dynamic scope?

What is lexical scope and dynamic scope?

In programming, a “scope” refers to the visibility and accessibility of variables and other identifiers within a program. There are two main types of scope in programming languages: lexical scope and dynamic scope.

Lexical scope, also known as static scope, refers to the way in which identifiers are bound to their definitions at the time the program is written, rather than when it is executed. In other words, lexical scope is determined by the physical location of the identifier and its definition in the source code of the program.

For example, consider the following code snippet written in a lexically scoped language such as PHP or JavaScript:

function outerFunction() {
$outerVariable = 'outer'; // Define $outerVariable within outerFunction

function innerFunction() {
global $outerVariable; // Reference $outerVariable from the global scope
echo $outerVariable; // Outputs 'outer'
}

innerFunction(); // Call innerFunction
}

outerFunction(); // Call outerFunction

In this example, the variable $outerVariable is defined within the outerFunction and is therefore only visible within that function. However, the innerFunction is able to access $outerVariable using the global keyword, which allows it to reference variables from the global scope (i.e., the outermost level of the program). As a result, when innerFunction is called, it is able to output the value of $outerVariable as ‘outer’.

Lexical scoping is a common feature of many programming languages, and is often used to create a hierarchy of nested scopes within a program. This can be useful for organizing and organizing code, as well as for limiting the visibility and accessibility of identifiers to specific parts of the program.

Dynamic scope, on the other hand, refers to the way in which identifiers are bound to their definitions at runtime, rather than at the time the program is written. In other words, dynamic scope is determined by the sequence of function calls within the program, rather than by the physical location of the identifier and its definition in the source code.

For example, consider the following code snippet written in a dynamically scoped language such as Common Lisp:

(defun outer-function ()
(let ((outer-variable 'outer)) ; Define $outerVariable within outerFunction
(defun inner-function ()
(print outer-variable)) ; Reference $outerVariable from the dynamic scope
(inner-function))) ; Call innerFunction

(outer-function) ; Call outerFunction

In this example, the inner-function is able to access the outer-variable defined within the outer-function using the dynamic scope, rather than the lexical scope. This is because the inner-function is called within the outer-function, and therefore has access to the variables defined within it. As a result, when inner-function is called, it is able to output the value of outer-variable as ‘outer’.

Dynamic scoping is less common than lexical scoping in modern programming languages, and is often used in languages that are intended for use in interactive environments, such as Lisp. Dynamic scoping can be useful for creating more flexible and interactive programs, but can also make it more difficult to understand and maintain code, as it can be harder to predict how variables will be bound and accessed at runtime.