In computer science, a lexically scoped (or statically scoped) programming language is one in which the free variables in a procedure are evaluated in the environment at the procedure definition. For example:
let a = 5
in let p = proc () a
       a = 10
   in (p)
will return 5 in an interpreter with lexical scoping, since the variable a is bound to the value 5 when p is defined.

Lexical scoping is slightly more difficult to implement than dynamic scoping, but it makes programs much easier to understand. Almost all modern programming languages are lexically scoped.

Compare dynamic scoping.

As opposed to dynamic scope, where bindings to variables in one procedure are visible in (assuming no further rebinding) all the procedures it calls, all procedures they call, etc. Dynamic scope has among its many disadvantages the problem that it is impossible (in the sense of `halting-complete') to ensure at compile time that variables are used only where they are in scope. This is perhaps the primary reason that most real programming languages nowadays use lexical scope.

When you combine lexical scope with functions as first-class objects (or, in general, escaping functions), you get (hopefully) closures. Not only do closures resolve free variables from the environment in which they were created---they do so even when called from outside that environment.

Languages with lexical scope include:

Log in or register to write something here or to contact authors.