Functional programming is a style of programming that emphasizes the evaluation of expressions, rather than execution of commands. The expressions in these language are formed by using functions to combine basic values. A functional language is a language that supports and encourages programming in a functional style.
Functional programming is a computer programming paradigm, and a family of programming languages, in which the fundamental unit is the function in the mathematical sense –an operator that takes as input one or more parameters and returns (usually) one result value. This is in contrast with the imperative programming model that is used in most popular languages, where the fundamental unit is the statement, which commands the machine to change its state in a specified way. See also the Comparison of programming language types.
Though most imperative programming languages have some concept of functions, functional programming languages have several features that go further:
- "Side effects" and stored variable values are entirely absent in "pure" functional languages or regarded as a feature of secondary importance in "impure" functional languages. Global variables that contain values that can affect other parts of the program are a bad thing, so why not do away with variables entirely, and just work with functions that return values. A functional program may contain no assignment statements at all, all values being computed when needed as function results.
- "First class functions" or "higher order functions" – functions are "first class" citizens of the type system and have all the rights that simpler types such as integers have. They can be passed as parameters to functions, they can be returned as result values by functions, and can be generated at runtime by functions. This has the consequence that there can be "anonymous functions", as a generated function will not have a declared name.
- Currying functions, an operation named in honour of Haskell B. Curry is also often found in functional programming. Closures are a related concept. Environment capture, variables from any of the scopes in which the function is being defined, is a related idea. Lazy evaluation is also common.
Recursion is often used in functional programming instead of imperative programming's looping structures.
Functional programming languages
LISP is one of the first programming languages (dating back to 1958), and is based on the Lambda calculus. It is still influential, and two major dialects, COMMON LISP and Scheme are still in use. Its syntax (prefix notation and brackets, and that's about it) is both its strength and its handicap.
ML and descendants SML, CAML and Objective CAML have a more familiar infix syntax.
Haskell and Miranda are pure functional languages. SML and Scheme are impure functional languages.
Functional programming languages are much further from the bare metal of the processor than imperative languages, and were originally interpreted. However they need not be interpreted any more, although compiling them is not nearly so straightforward as imperative languages which are modelled more directly on the underlying processor.
It would seem that there would be performance penalties to the functional approach. However this is offset by a higher-level program description, and the referential transparency gained by not having variables permits optimisations that are not possible in imperative programming languages. Functional languages can execute quickly.
Sources:
My long-ago studies,
the FAQ for comp.lang.functional