CodeNewbie Community

Cover image for What is Functional Programming?
Daniel Ziltener
Daniel Ziltener

Posted on

What is Functional Programming?

So you are interested in this fancy thing called functional programming. Good! Because that is what I want to teach you. Functional programming has been around for many decades, but only quite recently became the "hot new thing". And due to that, unlike so many other "hot new things" that happen to be bloated, overly complicated solutions to problems nobody really has, it is a quite simple, straightforward approach.

But why functional programming?

Functional programming is a paradigm that emphasizes simplicity. Unlike object oriented programming, it separates data and logic, and in its pure form tries to keep logic and mutable state apart as good as possible, facilitating reasoning about the code, debugging, and testing.

But today, we won't actually write code yet. Today, we want to get you into the mindset and thinking patterns to make the first lines of code as easy as possible for you!

No really, what is it?

Okay, enough with the rambling about the good old days. What actually is functional programming? Well, what the community generally understands as functional programming these days is often synonymous with "pure functional programming". It is a programming paradigm that takes a lot of ideas from how mathematics work. But don't worry, you don't need any kind of degree or to be especially talented in maths to learn it!

...Oh, looks like we got a visitor! It's Vodblob McSteele. Vodblob is an absolute expert in multiple areas of programming and got famous through multiple books and courses, and decided to help me out in this one.

Vodblob adjusting its glasses

The alien adjusts its glasses Nice to meet you! I'm Vodblob, I dedicated my life to studying all programming paradigms in existence. Some joke there is one for every eye I have, but so far there are still less. But today I am here to teach you functional programming! I can give you a few examples that make you think like a functional programmer in no time - without a single line of code to write! It takes a little slate and a chalk pen out of its pocket and draws two separate circles on it.

Vodblob points at the left circle The base idea is that you work with functions that take one (or more) argument, give a value back, and do nothing else. We call those pure functions, and for our explanation, they are all inside that left circle. A real life example for this would be your water heater in the office: you put in a liter of cold water, wait a bit, and get a liter of boiling water to pour into your tea cup. Always the same process. Unless the power is out - then you get nothing. But that applies to programming, too!

But the problem with that approach is that when you only have pure functions, you get a program that takes an input and produces an output and some hot air - and nothing else. Usually you need more than that, because usually, you want that program to interact with you or with some changing data.

It points at the right circle For that, we have impure functions. Those do more than the pure ones discussed above: they might give you a different result each time, and they might even change the program state, display something on your screen, or other things in the program's environment, like updating file contents! A bit like eating chips: each time you reach into the bag, you get out a slightly different chip, you modify the bag's state, and at some point you might even reach in and find - nothing!

As you've seen, I drew two distinct, separate circles to represent these groups, and for good reason. We want to keep those two categories separate unless interaction is absolutely necessary, because it is much easier to reason about, test, and debug pure functions and chained-together pure functions.

Vodblob takes a sip of tea out of its cup and pauses for a few seconds. It then takes the chalk pen again and draws a circle that encompasses both other circles.

Lastly, functional programming is data centric. Your program state tends to be just data structures - things like numbers, text strings, lists, and key-value maps -, and you use your vast collection of functions to extract, modify, and change the state, and make decisions based on these structures. This is the common denominator between the pure and impure functions, and their way of communicating. It is the background noise of the little universe the pure and impure bubbles live in.

Functions themselves can be data, too - you can store functions inside these data structures, and pass them as arguments to other functions (which then become higher-order functions). This last part is what allows you to do elegant data manipulation by simply combining functions you already have, and have them be applied to all kinds of data. This is very powerful!

Recap

Vodblob harrumphs, thinks for a few seconds, and continues.

Alright. I think this is it for today. It might be, purely looking at the number of words, a short lesson, but it is a lot of stuff to digest. To recap:

  • Pure Function: A function that, given X, always returns Y and does nothing else. Example: 2+2=4.
  • Impure Function: A function that, given X, might give changing results and changes the program state or the environment. Example: display text, write file, increase counter.
  • Data Structure: The way data is kept in memory. This could be text, numbers, lists, key-value-maps, ...
  • Program State / Environment: The former is mutable data inside your program, the latter is everything from stuff in databases you program can access to what's shown on screen.
  • Data centric: your program state is just data structures, and your program uses that data to make decisions and alter said data.
  • Higher-order functions: Functions that take other functions as argument to operate on data.

What now?

You might not have 100% understood a few things, but that is fine. You got a first insight, some food for thought, and the rest will come naturally as we progress.

Next time, we will look at a functional programming language and start writing simple code. Until then, your homework is to look at the recap and let your mind run wild. What can you imagine doing with the possibilities listed above? What data structures can you imagine existing? Be creative! It doesn't have to be realistic, after all you don't know yet what the languages actually work like.

Vodblob wipes his slate clean, packs his backpack, and shakes your hand.

Oh to be a blank slate again, to learn it all again with fresh eyes... Well, in any case, we'll hopefully see each other again soon!

Vodblob driving off into the sunset in its car

Discussion (0)