Non-local variable: Difference between revisions

Content deleted Content added
Add javascript example
Ruud Koot (talk | contribs)
before this turn into a source code repository again...
Line 1:
In [[programming language theory]], a '''non-local variable''' is a variable that is not defined in the local scope. While the term can refer to global variables, it is primarily used in the context of [[nested function|nested]] and [[anonymous function]]s where some variables can be neither in the [[local scope|local]] nor the [[global scope]].
 
== Examples == <!-- Please do NOT add the same example in other languages to this article. These examples suffice to get the point across. Wikipedia is not a source code repository. -->
== Example ==
=== Nested functions === <!-- Please do NOT add the same example in other languages to this article. These two examples suffice to get the point across. Wikipedia is not a source code repository. -->
In the Python 3 example that follows there is a nested function <code>inner</code> defined in the scope of another function <code>outer</code>. The variable <code>x</code> is local to <code>outer</code>, but non-local to <code>inner</code> (nor is it global):
<source lang=python>
Line 25 ⟶ 26:
</source>
 
=== Anonymous functions === <!-- Please do NOT add the same example in other languages to this article. These examples suffice to get the point across. Wikipedia is not a source code repository. -->
In the Haskell example that follows the variable <code>c</code> is non-local in the anonymous function <code>\x -> x + c</code>:
<source lang=haskell>