What Is Functional Programming?

It’s not actually about functions!

--

There are two main programming paradigms — imperative and functional. These two approaches differ significantly, and their names confuse everyone! In this article, we’ll explain everything in detail.

Is functional programming about functions?

Actually, no. In this context, the word “functional” has nothing to do with functions. Almost every programming language will feature functions in some form. The difference between functional and imperative programming lies in their approach to solving problems.

A metaphor: instructions vs. a rule book

Let’s imagine you’ve started a restaurant. Right now you have two kinds of employees: kitchen staff and managers.

You provide your cooks with clear step-by-step cooking instructions for a delicious and nutritious cabbage soup:

  1. Pour two cups of water in a pot
  2. Put the pot on the stove
  3. Add two teaspoons of salt to the pot
  4. For 10 servings, take one head of cabbage from the fridge
  5. Cut the cabbage in half
  6. […and so on]

You want the cook to follow the recipe in the exact order you intended. They can’t start cutting cabbages without taking them from the fridge. They shouldn’t add salt to a pot without adding water first. You’ve defined the precise order of the recipe. This same concept applies when we’re talking about imperative programming. You’re the one in control of the program, and it’s your instructions that will dictate the program’s execution.

Meanwhile, you give a book of rules to your manager:

  • Customers can’t bring their own food and drinks. Further, if they manage to sneak something in, you are obliged to tell them they must remove the items or ask them to leave.
  • The restaurant’s dining area must be clean. If it’s dirty, it’s the manager’s duty to inform the cleaners.
  • If there are too many guests for the waitstaff to handle, the manager should help them.

Now, to be totally clear, these are also instructions. However, the difference here is that the manager might not necessarily follow them in this particular order. Instead, they will take a particular action depending on the circumstances. This is similar to functional programming, while imperative programming follows the rule book approach demonstrated earlier with the recipe example.

That was just a simple explanation of the principle, so let’s explore the idea in more detail.

Imperative programming

Let’s talk about how imperative programming works. Imperative programming languages include C, С++, Go, Pascal, Java, Python, and Ruby.

In imperative programming languages, statements manipulate variables to solve explicit tasks, like working out how much tip you should leave at a restaurant. Commands run in the order they’ve been written (though imperative programming isn’t unique in this), and often rely on loops that modify a variable on each iteration.

There are variables that can store data and change at runtime. A variable acts as a container for data. We can create any type of variable, place a value in it, and then change the value to another one.

If you pass a value to a loop or function as an input, the result will depend both on the input data and on other variables. Let’s say we have a function that returns a discount when you buy something in an online store. When we add a $200 product to the cart, the function returns the discount. However, if the discount depends on the day of the week, the function will first check what day it is and then it’ll find the right discount in the table.

As we can see, the function takes $200 and returns different values on different days. In imperative programming, everything depends on variables.

The programmer determines the order that statements run. This means that the programmer defines the conditions under which the program operates. All of the logic is defined by the programmer. As such, a programmer can accurately predict at what point a certain piece of code will be executed. Using an imperative approach makes your code’s logic predictable and easy to work with.

If we have some code for calculating a discount that will fire specifically when a customer places an order, then the code will be executed only at that moment. It won’t calculate the discount in advance, nor will it calculate it after the fact. Just as you instructed, it will do it when a customer places an order.

👉 The essence of imperative programming is a clearly defined sequence of instructions that lead to the desired result.

So far, everything seems logical. In fact, the majority of programmers are accustomed to this type of behavior from their code. However, functional programming is a completely different beast — let’s step into its lair.

Functional Programming

Examples of functional programming languages are JavaScript, Ruby, Haskell, Lisp, Erlang, Clojure, and F#.

The idea behind functional programming is that instead of passing a sequence of commands, we describe how these commands should interact with functions and statements. This is similar to how objects work in object-oriented programming (OOP). However, in functional programming, this logic is implemented at the program level.

To illustrate that last point a little more, in OOP we create objects and define how they should interact with each other, and we write the methods the script needs to manipulate objects. It’s as if the methods stand aside, controlling how the program functions: it makes one object interact with another, processes results, and so on.

As for functional programming, we take this one step further. In this case, our code serves as a list of rules for interacting with data. The programmer’s responsibility is simply defining all the necessary rules. After that, the code will figure out how to apply them.

If we compare the principles behind the functional and imperative approaches, we’ll see that the only thing that they have in common is that they both have commands that a language can execute. Everything else is different.

Commands can be grouped into functions and statements and their order doesn’t matter. Since these functions and statements are rules that are usually applied only when they are needed, the order in which they are written is usually not important.

There are no variables. Don’t get us wrong, there are variables, just not as we know them. In a functional language, a variable can be declared only once. After that, the value of the variable can’t be changed. We deal with the same principle whenever we’re working with constants. Once you declare one, you can only read its value. As for values, they are stored in functions. All you have to do to access the desired value is to access the right function.

Passing a function the same data should always return the same result. In the previous example, we passed $200 to a function that returned a discount based on the day of the week. Functional programming is a little different. Here, functions must be stateless: if a function takes $200 as a parameter, it should return the same discount regardless of other variables. If we need the program to behave differently on specific days of the week, we’ll need to write another function for that.

Let’s draw an analogy with sine in math. No matter when you calculate the sine of 90 degrees and what angles you have in your problem, the sine of 90 will always equal 1. It’s the same deal with functional programming. Everything is predictable and dependent on input parameters.

Functions and statements are executed in an order that is defined by the code and the compiler. Each command is some kind of a rule. As such, it doesn’t matter where the rule is written — whether it be at the beginning or the end of your code. As long as you have the rule written, the compiler will be able to figure out when to apply it.

That’s also how languages work, with their spelling and grammar rules. No matter which order we learn these rules, the important thing is that we know how and when to use them. For example, a student learning English could theoretically learn simple conjugation in any order — past tense before future tense, or the other way around.

👉 To sum everything up, the essence of functional programming comes from writing a set of instructions that the compiler will refer to in order to achieve the desired result.

At Practicum, we offer online education and mentoring to help you develop your career in tech. Beginners are welcome!

--

--

TripleTen: Part-Time Online Tech Bootcamps
TripleTen Bootcamp

Learn The Job, Get The Job 📈 Online, Part-Time Tech Bootcamps 💡 87% of our grads get hired in 6 months or less 🚀