You may not have heard about Functional Programming. But you use it every day

The name is complex, but the concept is simple. Learn what Functional Programming is with Real-Life examples. Understand why it is widely used, even in non-IT environments, and how you can benefit from it.

You may not have heard about Functional Programming. But you use it every day

Everyone is a programmer. Well… Maybe not a professional one, but everyone programs in one way or another.

You don't believe me? Whenever you are…

  • Telling a recipe to your friend
  • Teaching your child how to use the coffee machine
  • Giving directions to a stranger on the street

…You are programming.

In the above cases, you are telling someone to do something. The same way a programmer tells a computer to do something.

When you program, you usually give instructions to someone:

  1. Beat 2 eggs
  2. Heat the pan
  3. Pour the eggs into the pan
  4. etc.

That's the most common programming style (paradigm), and it's called Imperative Paradigm.

However, there is another paradigm that, for sure, you also use a lot. It's the Functional Paradigm. And that's what I'll talk about today.

Instead of using instructions (do this, do that), we will use functions. But first, we need to know what functions are.

Understanding functions

When we are at school, functions are defined in a scary way. And it's usually something like this:

This was my first day at university… Monday, 7 AM, 200 people were in the classroom, and another 50 were outside because there was not enough space. The professor began explaining functions that way. I wanted to run away. But I didn't... Not because of my resilience but because it was overcrowded, and I couldn't find my way out.

No wonder people are not interested in Math. That's hard to understand, even for those who love numbers.

Forget about that graphic now and think of a function as the process through which something is transformed or created.

For example:

  • If you put a slice of bread into your toaster, you get a toast. So, the toaster has the "Toast" function.
Function "Toast"
  • When you put flour, water, and butter in your bread maker, you get a piece of bread. We can call this function "Make Bread").
Function "Make Bread"
  • Your clock gives the time. That's its function. In this case, it doesn't need any input to do that.
Function "Time"

We can conclude that a function is anything that receives something and returns something else. In exceptional cases, like the clock, it doesn't need to receive anything.

Now, let's put some names to what the function receives and returns:

Whatever a function receives can be called input, arguments, or parameters.

Similarly, what it returns can be called output or result.

One of the exciting things about functions is that they work together. We can execute different functions one after the other to produce more significant results. This is called Function Composition.

Function composition

Let's suppose that we want to make a toasted slice of bread. We know we have both the "Make Bread" and "Toast" functions.

Composing functions is nothing more than executing them sequentially, and the first function's output is the second one's input.

In our case, we compose "Make Bread" and "Toast" to make a toasted slice of bread:

Composing "Make Bread" and "Toast" functions

You can compose as many functions as you want and introduce new parameters along the way. So we can include here the "Spread Jam" function and add a new parameter (jam), so we end up with a toasted slice of bread with jam.

Composing "Make Bread", "Toast", and "Spread Jam" functions.

So What?

You may be thinking: Ok, I understand what you say, but how does all this apply to me, that I am not a professional programmer?

Well, one of the best applications ever created uses Functional Programming. That is MS Excel.

Excel formulas are functional programs. They contain only simple and composed functions.

You may have worked with Excel sheets and used this programming paradigm, even unconsciously.

Let's see some examples:

concat function concatenates the input parameters. In this case, Name (cell A2), space and Last Name (B2):

Untitled
Simple function

When compounding concat with left we get a new string. In this case, only the first letter of the name is displayed:

Untitled
Composing 2 functions

And we can achieve more complex results by applying more compositions.

In this example, we want to add leading zeros to the number in A6 so it matches the number of digits in B6.

Untitled
Complex function composition

Knowing about functional programming will help you to understand how Excel works and how to use it better.

In future newsletters, I'll introduce the basics of Excel and will include some exercises so you can improve your skills.

Conclusion

A function takes some input and transforms it or creates something new.

We can program using functions, and it's called Functional Paradigm.

We all do it, even unconsciously, when working with Excel formulas.

Compose functions to create more significant results.

We will see some of the basics of Excel in future deliveries.