What is functional programming about?

Published on Thursday, 14. October 2021

When I first learned about functional programming, I was helplessly confused. At that time I mostly did game development with Unity and C#. When I heard someone mention functional programming and read the Wikipedia article about it, I didn't understand why it was so special. What I took away from reading the article back then was that functional programming is a programming paradigm that uses functions as their building blocks. So what? C# (or any other procedural programming language for that matter) has functions as well. It took a course I took during my studies of computer science for me to understand it.

What was causing my confusion was that the observation that functional programming is made up of functions totally misses the point. They still are an important piece of the puzzle. In a mathematical sense, a function takes some input parameters and returns some values based on them. For example, the function \(f(x) = x^2\) takes one number x and returns its square. In programming languages, the term function is used a bit more widely. Here, functions not only accept and return some values, but can also have side effects. Side effects are all the things that have an effect outside of the program. Examples are printing something to the screen, writing to a file, or starting another process. Ultimately, everything the user of a program sees or cares about is a side effect.

Side effects are making programming more complicated. They essentially mean that each program has a hidden state that changes how it behaves. Depending on this state the program might return different results, even if the input parameters were exactly the same. The idea behind functional programming is to remove this hidden state. This makes the code easier to understand, easier to work with and allows for some great concepts. It also makes some problems impossible (or really hard) to solve. To get the best of both worlds, many procedural languages started to support concepts that originated in functional programming languages. But even though that's the case, I only started to appreciate them once I had worked with Haskell. So it still makes sense to do that. It will change the way you think about programming.