Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Nested function
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Attributes == The [[Scope (computer science)|scope]] of a nested function is the block that contains it {{endash}} be it a function block or block within a function body. It is not visible (cannot be called by name) outside its containing block. A nested function can use [[identifiers]] (i.e. the name of functions, variables, types, classes) declared in any enclosing block, except when they are masked by inner declarations with the same names. A nested function can be declared within a nested function, recursively, to form a deeply nested structure. A deeply nested function can access identifiers declared in all of its enclosing blocks, including enclosing functions. Nested functions may in certain situations lead to the creation of a [[Closure (computer programming)|closure]]. If it is possible for the nested function to [[Escape analysis|escape]] the enclosing function, for example if functions are [[first class object]]s and a nested function is passed to another function or returned from the enclosing function, then a closure is created and calls to this function can access the environment of the original function. The frame of the immediately enclosing function must continue to be alive until the last referencing closure dies and [[non-local variable|non-local]] [[automatic variable]]s referenced in closures can therefore not be [[stack allocation|stack allocated]] in languages that allow the closure to persist beyond the lifetime of the enclosing block. This is known as the [[funarg problem]] and is a key reason why nested functions was not implemented in some simpler languages as it significantly complicates code generation and analysis, especially when functions are nested to various levels, sharing different parts of their environment.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)