Defining Functional Programming
Learn the definition of functional programming.
Functional programming
Functional programming is a programming paradigm. Unique to functional programming is the writing of pure functions, free of side effects and side causes. Functions are key elements in functional programming and are at the center of the paradigm. Computations in functional programming are based on composing mathematical functions to modify the state, or the values, of all programming variables at a given point in time. Functional programming draws most of its hypotheses and mathematics, in general, from category theory.
Functional programming has many siblings, including object-oriented (OOP), imperative, declarative, and reactive programming. Each programming paradigm has unique characteristics. To put this into perspective, consider each of functional programming’s siblings:
-
Object-oriented programming is class and object-driven.
-
Imperative programming is based on side effect inducing statements.
-
Declarative programming is predicated on describing action statements.
-
Reactive programming is asynchronous and stream-oriented.
Our first problem
Consider a problem as simple as adding two numbers. Computing the sum of two operands can be done by abstracting away the mysterious addition statement. We achieve this abstraction by using a function.
<?phpfunction add(int $x, int $y): int{return $x + $y;}echo add(1, 2); // evaluates to 3
Functional programming is a paradigm that uses mathematical functions to evaluate the state of a program.
Functional programming is a declarative paradigm
Functional programming is a
Note: Don’t assume that abstraction completely eliminates complexity. That’s a misinterpretation of the concept. According to the theory, complexity should be carefully divided into separate functions, rather than deleted from code, as that negates the purpose of writing useful operational programs.
Expressions
Expressions are blocks of code that evaluate to a value. They typically take on the form of either function calls or operations with any number of operands. Here are a few examples of expressions in PHP:
-
2 * 2;
-
array_filter(range(1, 5), fn (int $x): bool => $x % 2 == 0);
-
'foo' . 'bar';
-
SplFixedArray::fromArray(['foo', 'baz']);
Expressions can be saved in memory by putting them in a container with a name. In fact, you probably already do this all the time, and it’s a habit we should continue when building functional programming apps.
Assignment in functional programming is very deliberate
In functional programming, assignment is a memoization vehicle—a means of storing the result of an expression for future invocations. It may not always be a requirement in execution sequences or even some function signatures. Contextually, expressions that are not reusable in the code that proceed it often need not be memoized as they do not merit the additional (though often trivial) memory cost. Such is the succinctness of functional programming.