I googled “functional programming swift” and the similar. Most of the articles on this topic create more questions than answers. Authors often skip important definitions and principles, and starts directly from .map-.reduce tutorial. Skipping the concept steals important building blocks, which leads to unstable foundation.

If a topic could not be explained in a freshman lecture, it was not yet fully understood.
Richard Feynman

The definition

The Functional programming definition is widely discussed over the Internet. After some research, I found the article, which I strongly recommend for your attention. It contains different Functional programming definitions analysis.

As you see, it’s hard to give the “one true” definition. Wikipedia one in my opinion quite precise but needs some explanations.

Functional programming (FP) is a declarative programming paradigm, which means programming is done with expressions. In functional code, the output value of a function depends only on the arguments that are input to the function, so calling a function f twice with the same value for an argument x will produce the same result f(x) each time. Eliminating side effects, i.e. changes in state that do not depend on the function inputs, can make it much easier to understand and predict the behavior of a program, which is one of the key motivations for the development of functional programming.

Let’s decompose it into small blocks, and try to make them understandable.

Declarative programming paradigm

Saying with simple words, you use Declarative programming when you write your code in such a way that it describes what you want to do instead of how you want to do it. It’s up to the compiler to figure out the how.

The simplest example of declarative programming from iOS developers life is the Autolayout. Using the Autolayout you only declare rules, or, in other words, tell compiler What you want, e. g. the view A should always be top-left and should be equal width with view B. Compiler understands the rules, and creates instructions for views management. On the other hand, you can manage views frames by yourself, using Imperative approach.

Functions and Side effects

In mathematics, a function is a relation between a set of inputs and a set of permissible outputs with the property that each input is related to exactly one output.

In programming terms, the function is a self-contained chunk of code, which can accept arguments and return results. If the function has no side effects, the result of multiple evaluations with the same arguments will be the same.

We say Function has side effects, if it’s evaluation changes state outside of the function scope. Like the class instance property modification, modification of global variable, e. t. c. Function without side effects does not impact the global state. This is a huge benefit, because of program behavior predictability and optimization opportunities.

Sticking everything together

So, the Functional programming is a programming paradigm, when you tell compiler what to do, instead of writing exact instructions of how to do. For telling the compiler what to do, you use functions, which behavior is always predictable and does not change the global state.

Good news, we can use the functional approach in non-functional languages, including Swift.

P. S.

This article does not cover the wide range of FP benefits, does not tell anything about techniques of functional programming.