Non-local variable: Difference between revisions

Content deleted Content added
KZ737 (talk | contribs)
 
(31 intermediate revisions by 24 users not shown)
Line 1:
{{Short description|In computer programming, a variable which is not defined in the local scope}}
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]].
 
{{More citations needed|date=January 2025}}
== Example ==
In [[programming language theory]], a '''non-local variable''' is a variable that is not defined in the local [[Scope (computer science)|scope]]. While the term can refer to [[global variablesvariable]]s, it is primarily used in the context of [[nested function|nested]] and [[anonymous function]]s where some variables can be neither in neither the [[local scope|local]] nor the [[global scope]].
 
In [[Lua (programming language)|Lua]] they are called the ''upvalues'' of the function.<ref>''[http://www.lua.org/pil/contents.html Programming in Lua (first edition)],'' "[http://www.lua.org/pil/27.3.3.html 27.3.3 – Upvalues]"</ref>
 
== 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. -->
=== 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):
<sourcesyntaxhighlight lang=python"python3">
def outer():
x = 1
Line 11 ⟶ 17:
print(x)
return inner
</syntaxhighlight>
</source>
 
In JavaScript, the locality of a variable is determined by the closest <code>var</code> statement for this variable. In the following example, <code>x</code> is local to <code>outer</code> as it contains a <code>var x</code> statement, while <code>inner</code> doesn't. Therefore, x is non-local to <code>inner</code>:
<syntaxhighlight lang="javascript">
function outer() {
var x = 1;
function inner() {
x += 1;
console.log(x);
}
return inner;
}
</syntaxhighlight>
 
=== 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>:
<sourcesyntaxhighlight lang="haskell">
outer = let c = 1 in map (\x -> x + c) [1, 2, 3, 4, 5]
</syntaxhighlight>
</source>
 
== Implementation issues ==
{{see also|Nested function#Implementation|Man or boy test}}
Non-local variables are the primary reason it is difficult to support nested, anonymous, [[higher-order function|higher-order]] and thereby [[first-class function]]s in a programming language.
 
If the nested function or functions are (mutually) [[Recursion (computer science)|recursive]], it becomes hard for the [[compiler]] to know exactly where on the [[call stack]] the non-local variable was allocated, as the [[frame pointer]] only points to the local variable of the nested function itself and there can be an arbitrary number of [[activation record]]s on the stack in between. This is generally solved using [[access link]]s or [[display register]]s.
 
If the nested function is passed as an argument to a higher-order function a [[closure (computer science)|closure]] needs to be built in order to locate the non-local variables. If the nested function is returned as a result from its outer function (or stored in a variable) the non-local variables will no longer be available on the stack. They need to be heap allocated instead, and their lifetime extendextends beyond the lifetime of the outer function that declared and allocated them. This generally requires garbage-collection.
 
==Notes==
{{reflist}}
 
== References ==
Line 29 ⟶ 52:
 
[[Category:Programming language theory]]
[[Category:Variable (computer programmingscience)]]