What is functional programming?#
Functional programming is centered around building software composed of functions, similar to procedural programming; however, there are subtle differences. We highlight these differences by discussing key concepts of functional programming below.
First-class citizens#
Functions are treated as primitives. Primitives serve as the simplest elements of a programming language. Thus, a function can be:
- stored in a variable
- passed as an argument
- returned from a function
Since they have these capabilities, functions are referred to as first-class citizens. The term first-class citizen essentially notates an element that supports all other generally available operations.
Pure functions#
A goal of functional programming is to write pure functions whenever possible.
A pure function does not change the global state of the program and therefore is said to have no side effects. Instead, its return value is solely based on its input parameters.
Pure functions enable numerous compiler optimizations, leading to significantly reducing a program’s execution time. Furthermore, pure functions are ideally suited for unit testing. We can unit-test a pure function in isolation without having to worry about where in the code it will be used.
Higher-order functions and recursion#
Instead of iterative loop constructs, such as for
and while
, in functional programming, we use higher-order functions and tail calls for a recursive loop.
A higher-order function takes a function as its input, performs an operation on the input function, and subsequently returns a function.
A tail call refers to a function call at the end of a function. A tail call may result in a recursive loop when a function calls itself. Many compilers perform tail call optimization (a.k.a. tail call elimination) making recursion as fast as iterative loops.
Key reasons for preferring recursion over typical primitive loops are:
- simplicity of code
- avoiding mutable objects to make code more robust
Immutability#
Immutability is the permanence of a variable once the data is assigned to it.
No value assigned to a variable in functional programming can be changed; the only way to change a value is to create a new variable. It is immutability that enables functional programming to use pure functions. These functions do not mutate their inputs and rely on nothing but the input to achieve results. In this way, functional programming is similar to math. Functional programming was even created for an academic computer science setting through the Haskell language.
In summation, functional programming is a declarative programming model that communicates what the program does without specifying the flow of control. It uses pure functions, immutable variables, and tail-call recursion. This results in code that is easier to understand, debug, and unit-test. Furthermore, functional programming enables numerous compiler optimizations for an increase in a program execution speed and reduction in memory requirements.