Jump to content

Static scoping: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
No edit summary
Norm (talk | contribs)
Fix link
Line 22: Line 22:
Lexical scoping was first introduced in [[Algol programming language|Algol]], and has been picked up in other languages since then. In some families of programming languages you will see more modern versions use lexical scoping while older ones use dynamic scoping. For instance in [[Lisp programming language|Lisp]], [[Emacs Lisp]] once used only dynamic scoping, [[Common Lisp]] has both dynamic and lexical scoping, and [[Scheme programming language|Scheme]] uses lexical scoping exclusively. In other cases, languages which already had dynamic scoping have added lexical scoping afterwards. Examples include the already mentioned Emacs Lisp, [[Perl]], and [[Python programming language|Python]]. [[C programming language|C ]] and [[Pascal programming language|Pascal]] have always had lexical scoping, which is natural since they are both influenced by the ideas that went into Algol.
Lexical scoping was first introduced in [[Algol programming language|Algol]], and has been picked up in other languages since then. In some families of programming languages you will see more modern versions use lexical scoping while older ones use dynamic scoping. For instance in [[Lisp programming language|Lisp]], [[Emacs Lisp]] once used only dynamic scoping, [[Common Lisp]] has both dynamic and lexical scoping, and [[Scheme programming language|Scheme]] uses lexical scoping exclusively. In other cases, languages which already had dynamic scoping have added lexical scoping afterwards. Examples include the already mentioned Emacs Lisp, [[Perl]], and [[Python programming language|Python]]. [[C programming language|C ]] and [[Pascal programming language|Pascal]] have always had lexical scoping, which is natural since they are both influenced by the ideas that went into Algol.


See also: [[closure (programming)]], [[lexical]], [[dynamic variable scoping]].
See also: [[Closure_(computer_science)|closure]], [[lexical]], [[dynamic variable scoping]].

Revision as of 15:12, 9 August 2004

scope (programming).

In computer science, static scoping, as opposed to dynamic scoping, is a way that the scope (programming) of free variables is determined according to its position in program code. It is also called lexical scoping or lexically scoping.

A variable is said to be lexically scoped if its scope is defined by the text of the program. For instance a variable named balance might be scoped to the inside of the body of one function. That variable is then guaranteed to have nothing to do with any other variable named balance anywhere else in the program, or indeed with the variable named balance in other calls to the same function. This allows programmers to guarantee that their private variables will not accidentally be accessed or altered by functions that they call, and is considered a significant improvement over the older dynamic variable scoping

For example, take this procedure in Scheme:

(define yoricktestprogram
  (lambda ()
    (let* ((x 5)
          (gimmex (lambda () x )))
      (let* ((x 10))
        (gimmex)))))

gimmex will return 5 in lexical scoping, and 10 in dynamic scoping.

In lexical scoping, gimmex was defined in the scope of the outer let*, where x is 5. gimmex closes the procedure (lambda () x) around the scope of x = 5. In other words, variables that occur free in a procedure or subroutine would be looked up in the scope where the procedure was declared. In dynamic scoping, gimmex would return 10, because when gimmex was called, x was defined to be 10.

Lexical scoping was first introduced in Algol, and has been picked up in other languages since then. In some families of programming languages you will see more modern versions use lexical scoping while older ones use dynamic scoping. For instance in Lisp, Emacs Lisp once used only dynamic scoping, Common Lisp has both dynamic and lexical scoping, and Scheme uses lexical scoping exclusively. In other cases, languages which already had dynamic scoping have added lexical scoping afterwards. Examples include the already mentioned Emacs Lisp, Perl, and Python. C and Pascal have always had lexical scoping, which is natural since they are both influenced by the ideas that went into Algol.

See also: closure, lexical, dynamic variable scoping.