Functional programming
For a different style of programming, see Procedural programming. In computer science, functional programming is a programming paradigm where programs are built by applying and composing functions. It is a declarative programming paradigm. Its functions are trees of expressions that turn values into other values. They are not a list of statements that change the running state of the program.
In functional programming, functions are first-class entities. They can be given names (including local identifiers), passed as arguments, and returned from other functions, just like any other data type. This helps programmers write programs in a declarative and composable way, where small functions are joined together in a modular way.
Functional programming is sometimes treated as the same thing as purely functional programming, which is a smaller part of it. Purely functional programming uses only deterministic mathematical functions, also called pure functions. When a pure function is given the same arguments, it always gives the same result. It is not changed by any stored state or other side effects. Other procedures, which are common in imperative programming, can have side effects, such as changing the program's state or taking input from a user. Supporters say that limiting side effects can mean fewer bugs, programs that are easier to debug and test, and programs that are better suited to formal verification.
Functional programming has its roots in academic work and grew out of the lambda calculus, a formal system of computation based only on functions. It has been less popular than imperative programming, but many functional languages are used today in industry and education. These include Common Lisp, Scheme, Clojure, Wolfram Language, Racket, Erlang, Elixir, OCaml, Haskell, and F#. Lean is a functional programming language that is often used for checking mathematical theorems.
Functional programming is also important in some languages that are successful in certain areas, such as JavaScript on the Web, R in statistics, J, K, and Q in financial analysis, and XQuery/XSLT for XML. Some declarative languages made for specific tasks, like SQL and Lex/Yacc, use some functional ideas, such as not allowing mutable values. Many other programming languages support a functional style or have added functional features. Examples include D (since 2007 with the release of D2), C++ (since C++11), C#, Kotlin, Perl, PHP, Python, Go, Rust, Raku, Scala, and Java (since Java 8).
History
The lambda calculus was developed in the 1930s by Alonzo Church. It is a formal system for computation built on function application. In 1937, Alan Turing showed that it and Turing machines can do the same kinds of computing. This means it is Turing complete. Lambda calculus is the foundation of all functional programming languages. A related theory, combinatory logic, was developed in the 1920s and 1930s by Moses Schönfinkel and Haskell Curry. Later, Church created a simpler system, the simply typed lambda calculus. It gave a data type to every term, and it became the basis for statically typed functional programming.
The first high-level functional language was Lisp. It was created in the late 1950s for IBM 700/7000 series computers by John McCarthy at Massachusetts Institute of Technology (MIT). Lisp used Church's lambda notation and added recursive functions. It was the first language to include many features of functional programming. Early Lisp languages were multi-paradigm languages, meaning they supported more than one style of programming. Later versions, such as Scheme, Clojure, Dylan, and Julia, tried to build a cleaner functional core. Common Lisp kept many features of the older versions it replaced.
The Information Processing Language (IPL), from 1956, is sometimes called the first computer-based functional programming language. However, it is an assembly-style language that works with lists of symbols and changes those lists often, so it is not really functional. Kenneth E. Iverson created APL in the early 1960s. He described it in his 1962 book A Programming Language (ISBN 9780471430148). APL had a big influence on John Backus's FP. In the early 1990s, Iverson and Roger Hui created J. In the mid-1990s, Arthur Whitney created K, which is used in the financial industry, along with its newer version, Q.
In the mid-1960s, Peter Landin invented the SECD machine. It was the first abstract machine made for a functional language. He also proposed the ISWIM language. In his 1977 Turing Award lecture, Backus presented FP. He described functional programs as built from "combining forms" that follow the principle of compositionality. His paper encouraged more research into functional programming, though it focused on function-level programming. The language ML was created in 1973 by Robin Milner at the University of Edinburgh. David Turner developed SASL at the University of St Andrews. ML later grew into other languages. The most common ones today are OCaml and Standard ML.
In the 1970s, Guy L. Steele and Gerald Jay Sussman developed Scheme. It was the first Lisp dialect to use lexical scoping and to require tail-call optimization. In the 1980s, Per Martin-Löf developed intuitionistic type theory. This led to new methods for interactive theorem proving. The lazy functional language Miranda, also developed by David Turner, appeared in 1985. It strongly influenced Haskell, which began in 1987 as an open standard for research into functional programming. More recently, functional programming has been used in special areas, such as the OpenSCAD language, which is built on the CGAL framework. It is still used in businesses today.
Concepts
Main articles: First-class function and Higher-order function
Main article: Pure function
Main article: Recursion (computer science)
Main article: Evaluation strategy
Main article: Type system
Main article: Referential transparency
Main article: Purely functional data structure
Some ideas in functional programming are special. They are quite different from imperative programming and object-oriented programming, though many languages mix these styles. Higher-order functions can take other functions as inputs or give them back as results. For example, the differential operator d/dx gives the derivative of a function. Currying means applying a function to one argument at a time. This lets the successor function be written as addition applied to the natural number one. Pure functions do not have side effects. They give the same result for the same inputs, which can help programs run faster. Functional programs often repeat actions with recursive functions instead of loops. A special kind of recursion called tail recursion can be turned into the same code a loop would use.
Functional languages also handle function inputs in different ways. With strict evaluation, all inputs are worked out before a function runs. With lazy evaluation, inputs are only worked out when they are needed. Many functional languages use type systems to check programs before they run. In functional programs, a variable's value does not change once it is set. This keeps programs predictable. Because of this, data structures are often built in different ways. For example, they may use maps instead of arrays. They can also keep older versions of themselves unchanged.
Comparison to imperative programming
Functional programming works very differently from imperative programming. The biggest difference is that functional programming avoids side effects. Imperative programming uses side effects to keep track of changing information and to handle input and output. Instead of using a loop to change a list, a functional program often uses a "map" function. This function takes a function and a list, and returns a new list. Functional code can also help avoid mistakes like off-by-one errors. These mistakes can be hard to find in large, complicated imperative code (see Greenspun's tenth rule).
Some tasks, like keeping track of a bank account balance, seem easiest when things change over time. Pure functional languages handle these tasks in a different way. The language Haskell uses monads, which come from category theory. Many students find monads hard to understand. Other functional languages pass unchanging information into functions and get back a new result. Languages such as Clojure allow changing information through managed references. Researchers have also created ways to track side effects, such as Hoare logic, uniqueness, and effect systems. Functional languages usually need more processing power and memory than imperative languages like C and Pascal. Still, languages like OCaml and Clean are only a little slower than C for tasks with lots of numbers.
Functional ideas can also help with doing many things at once. One way is message passing, as used in the actor model found in Erlang/Elixir and Akka. Lazy evaluation can make a program faster. However, if it is used the wrong way, it may cause memory leaks. Rust is known for its approach to unchangeable references.
Comparison to logic programming
Logic programming can be seen as a wider version of functional programming. In it, functions are a special kind of relation. A function has a strict set of inputs and outputs. A relation can be used in many directions. For example, the mother relation can be used to find a child's mother, or to find the children of a mother.
Functional syntax is a shorter way to write nested functions than relational syntax. But the two are mostly different ways of writing the same idea. In Ciao Prolog, relations can also be nested like functions. Ciao changes this function-like style into relational form before it runs the program.
Applications
Functional programming is used in many ways. Spreadsheets can be seen as a simple kind of functional programming system, with a zeroth-order style. However, spreadsheets usually cannot use higher-order functions or reuse code, and some cannot use recursion. Some extensions have been made for spreadsheet programs, but they are mostly still academic. Because of their composability, functional programming ideas can fit well with microservices-based systems. Functional programming is an active area of research in programming language theory. Researchers share their work in peer-reviewed venues such as the International Conference on Functional Programming, the Journal of Functional Programming, and the Symposium on Trends in Functional Programming. Many universities teach functional programming, and some teach it before other methods. It is also used outside computer science to teach problem-solving, algebra, geometry, and classical mechanics, as in the book Structure and Interpretation of Classical Mechanics. Scheme has been a popular choice for teaching programming for many years.
Functional programming has been used in many industries. Erlang was developed by the Swedish company Ericsson in the late 1980s. It was first used to build fault-tolerant telecommunications systems. It has since become popular at companies such as Nortel, Facebook, Électricité de France and WhatsApp. Scheme, a dialect of Lisp, was used for several programs on early Apple Macintosh computers. It was also used for tasks such as simulation software and telescope control. OCaml, introduced in the mid-1990s, has been used in financial analysis, driver verification, industrial robot programming, and checking embedded software. Haskell was first meant as a research language. It has also been used in aerospace systems, hardware design, and web programming. Other functional languages used in industry include Scala, F#, Wolfram Language, Lisp, Standard ML and Clojure. Scala has been widely used in Data science. ClojureScript, Elm and PureScript are used for frontend programming. Elixir's Phoenix framework is used by some popular commercial projects, such as Font Awesome and Allegro's classified ads platform Allegro Lokalnie. In finance, functional "platforms" have been popular for risk analysis, especially at large investment banks, where risk factors are written as functions. These systems are sometimes considered related to a categorical abstract machine. Functional programming is also strongly influenced by category theory. Some of these systems also resemble Gröbner basis optimizations and follow rules such as Comprehensive Capital Analysis and Review.

Quiz time!
4 questions about Functional programming
What are functional programs mainly built from?
Keep exploring
This article is a child-friendly adaptation of the Wikipedia article on Functional programming, available under CC BY-SA 4.0.