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!
== Alternatives == Various alternative techniques can be used to achieve similar programming results as via nested functions. === Modularity === A common alternative is to leverage a language's modularity technology. Some functions are exposed for use outside of the module and some are only visible within the module. In C, this can be implemented by declaring functions and variables as ''static'' to hide them from code outside the file.<ref name=cfaq>"[http://c-faq.com/misc/nestfcns.html Question 20.24: Why doesn't C have nested functions?], comp.lang.c FAQ</ref> This allows for data hiding, encapsulation and decomposition, but at a different level of [[granularity]] than with nested functions. This modularity does not support more than one level of nesting. In [[object-oriented languages]], a class typically provides a scope in which functions and state can be hidden from consumers of the class but accessible within the class. Some languages allow classes to be nested. === Parameters === To implement data hiding, functions can pass around shared data as parameters, but this increases the complexity of function calls.{{sfn|Bright|2004}} In C, this is generally implemented by passing a pointer to a structure containing the shared data.<ref name=cfaq /> === Lambda === In [[PHP]] and other languages, the [[anonymous function|lambda]] is an alternative. A function is defined in a code statement rather than declared with the usual function syntax. It has no name but is callable via a [[function reference]]. Such functions can be defined inside of a function as well as in other scopes. To use local variables in the anonymous function, use [[Closure (computer science)|closure]]. === Alternatives by language === The following languages provide features that are similar to nested functions: *[[C++]] {{endash}} classes allow for similar data hiding and encapsulation; defining a class within a class provides similar structure (see [[Function object#In C and C.2B.2B|Function object in C++]]) *[[C++11]] and later {{endash}} via lambda expressions (see quicksort example above)<ref>{{Cite web|url=http://www.rosettacode.org/wiki/Nested_function#C.2B.2B|title = Nested function - Rosetta Code}}</ref> *[[Eiffel (programming language)|Eiffel]] {{endash}} explicitly disallows nesting of routines to keep the language simple; does allow the convention of using a special variable, '''Result''', to denote the result of a (value-returning) function *[[C Sharp (programming language)|C#]] and [[Visual Basic .NET|Visual Basic]] {{endash}} via lambda expressions *[[Java (programming language)|Java]] {{endash}} since Java 8, via [[Anonymous function#Java|lambda expressions]]<ref>{{Cite web|url=http://www.rosettacode.org/wiki/Nested_function#Java|title = Nested function - Rosetta Code}}</ref><!--How can a lambda expression "simulate" nested scopes, in any number of levels, containing any language element (functions, variables, constants, types) ?-->, and in older versions, via an [[anonymous class]] containing a single method
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)