Content deleted Content added
m Reverted edits by 2601:1C2:4E02:AE60:A9CA:A01C:7B73:DEAB (talk) (HG) (3.4.10) |
m →=== Nested functions ===: fixed typo |
||
(6 intermediate revisions by 5 users not shown) | |||
Line 1:
{{Short description|In computer programming, a variable which is not defined in the local scope}}
{{More citations needed|date=January 2025}} 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 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>
Line 16 ⟶ 19:
</syntaxhighlight>
In
<syntaxhighlight lang="javascript">
function outer() {
Line 40 ⟶ 43:
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
==Notes==
{{reflist}}
== References ==
* Aho, Lam, Sethi, and Ullman. "7.3 Access to Nonlocal Data on the Stack". ''[[Compilers: Principles, Techniques, & Tools]]''. Second edition.
|