Non-local variable: Difference between revisions

Content deleted Content added
m dab stack => call stack
Line 21:
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) [[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 extend beyond the lifetime of the outer function that declared and allocated them. This generally requires garbage-collection.