- First-class function
In
computer science , aprogramming language is said to support first-class functions (or function literal) if it treats functions asfirst-class object s. Specifically, this means that the language supports constructing new functions during the execution of a program, storing them in data structures, passing them as arguments to other functions, and returning them as the values of other functions. This concept doesn't cover any means external to the language and program (metaprogramming ), such as invoking acompiler or aneval function to create a new function.These features are a necessity for the
functional programming style, in which (for instance) the use ofhigher-order function s is a standard practice. A simple example of a higher-ordered function is the "map" or "mapcar" function, which takes as its arguments a function and a list, and returns the list formed by applying the function to each member of the list. For a language to support "map", it must support passing a function as an argument.There are certain implementation difficulties in passing functions as arguments and returning them as results. Historically, these were termed the
funarg problem s, the name coming from "function argument". A different set of difficulties arises when programs can create functions at runtime; if the program is compiled, this means that the runtime environment must itself include a compiler.Availability
Languages which are strongly associated with functional programming, such as Lisp, Scheme, ML, and Haskell, all support first-class functions. Other languages which also support them include
Perl , Python,PHP , Lua, Tcl/Tk,ECMAScript (JavaScript ,ActionScript ), Ruby, Io, Scala, andNemerle .Most modern, natively compiled programming languages (e.g. C and Pascal) support function pointers, which can be stored in data structures and passed as arguments to other functions. Nevertheless, they are not considered to support first-class functions since, in general, functions cannot be created dynamically during the execution of a program. The closest analog would be a dynamically compiled function created by a
just-in-time compiler , which is compiled as an array ofmachine language instructions in memory and then cast to a function pointer. However, this technique is specific to the underlying hardware architecture and is, therefore, neither general nor portable. TheC++ programming language supports user-defined operators, including the '()' operator, which allows first-class objects to be treated as functions. Those objects can be manipulated just like any other first-class object in C++, but such manipulations do not include changing the function itself at runtime. Additionally, real Lambdas (see Lambda Calculus) have no language support in the last C++ standard (although there may be inC++0x ).Examples
= Lisp =ALGOL 68 PROC newton = (REAL x, error, PROC (REAL) REAL f, f prime) REAL: #
Newton's method # IF f(x) <= error THEN x ELSE newton (x - f(x) / f prime (x), error, f, f prime) FI; print(( newton(0.5, 0.001, (REAL x) REAL: x**3 - 2*x**2 + 6, (REAL x) REAL: 3*x**2 - 4**x), newline ));ECMAScript Note that the "callee" keyword in ECMAScript makes it possible to write recursive functions without naming them.
Python
Python does not have a (full) function literal, because the lambda keyword can only be followed by an expression and not statements.
However, functions can be created dynamically; they live in the same namespace as ordinary variables, and thus it is possible to assign a function to a variable:It is also possible to create a simple function which returns any function.
There is also possible to make code with same funcionality like in it is Javascript section below:
Javascript
Javascript supports both first class functions and
lexical scope .ee also
*
Anonymous function
*Closure
*Function object
*First-class object
Wikimedia Foundation. 2010.