Functional programming is a programming paradigm, meaning that it is a way of thinking about software construction based on some fundamental, defining principles. Some of these defining principles include composing pure functions, avoiding shared state, mutable data and side-effects. Other examples of programming paradigms include object oriented programming and procedural programming.
When learning to understand functional programming the main concepts that I struggled to wrap my head around was: Pure functions, Function composition, Avoid shared state, Avoid mutating state, Avoid side effects. If you are coming from object oriented programming you may understand what some of these terms might mean however, for the most part, this is pretty unfamiliar vocabulary. If you want to know what functional programming is you might want to start with those core concepts.
A pure function is a function in which when you are given the same inputs, the program will always return the same output with no side-effects. If you want to delve deeper into the real meaning of pure programming you can check out the article “What is a Pure Function?” for more information and detail.
Now something that you may be using in object oriented programming is something that is avoided in functional programming. Shared State! this is when any variable, object, or memory space that exist in a shared scope, or as the property of an object being passed between scopes. This is a pretty useful thing to have when programming. It makes sense as to why object oriented programming use, so the question stands, why should we avoid it in functional programming?
The problem with shared state is that in order to understand the effects of a function, you have to know the entire history of every shared variable that the function uses or affects.
For example, if you have a user object that needs saving. You may have a saveUser() function that makes a request to an API server. While thats happening the user changes their profile picture with the updatedAvatar() function and in turns triggers another saveUser() request. On the save, the server sends back a user object that should replace whatever is in memory in order to sync up with the changes.
However, the second response gets received before the first response, so now the new profile pic gets wiped out in memory and replaced with the one. This is a very common bug associated with shared stated. I’ve personally experienced this myself with trying to change my profile picture for my google account. I would change it but it would sometimes still show the old picture for a while.
There are many more concepts that I can get into for functional programming. But for now, as long as you know that functional programming favors pure functions instead of shared state you are off to a great start. If you would like to continue in your learning you can look at functional programming also favors immutability over mutable data, expressions over statements, and function composition over imperative flow control. Good luck on your journey!