Functional Programming is exactly what it says, functional. It creates clean and easy to read code that uses functions to efficiently do whatever is needed.
I really enjoyed reading the following sites explanation for Functional Programming, specifically the “split pea soup” reference.
Yes, dear, to make pea soup you will need split peas, the dry kind. And you have to soak them at least for a night, or you will have to cook them for hours and hours. I remember one time, when my dull son tried to make pea soup. Would you believe he hadn’t soaked the peas? We almost broke our teeth, all of us. Anyway, when you have soaked the peas, and you’ll want about a cup of them per person, and pay attention because they will expand a bit while they are soaking, so if you aren’t careful they will spill out of whatever you use to hold them, so also use plenty water to soak in, but as I said, about a cup of them, when they are dry, and after they are soaked you cook them in four cups of water per cup of dry peas. Let it simmer for two hours, which means you cover it and keep it barely cooking, and then add some diced onions, sliced celery stalk, and maybe a carrot or two and some ham. Let it all cook for a few minutes more, and it is ready to eat.
¶ Another way to describe this recipe:
Per person: one cup dried split peas, half a chopped onion, half a carrot, a celery stalk, and optionally ham.
Soak peas overnight, simmer them for two hours in four cups of water (per person), add vegetables and ham, and cook for ten more minutes.
¶ This is shorter, but if you don’t know how to soak peas you’ll surely screw up and put them in too little water. But how to soak peas can be looked up, and that is the trick. If you assume a certain basic knowledge in the audience, you can talk in a language that deals with bigger concepts, and express things in a much shorter and clearer way. This, more or less, is what abstraction is.
¶ How is this far-fetched recipe story relevant to programming? Well, obviously, the recipe is the program. Furthermore, the basic knowledge that the cook is supposed to have corresponds to the functions and other constructs that are available to the programmer.
This broke it down into a different way to understand the concept. Here is a link to that site: http://eloquentjavascript.net/1st_edition/chapter6.html
Here are some helpful hints I found for functional programming at: https://www.smashingmagazine.com/2014/07/dont-be-scared-of-functional-programming/
- All of your functions must accept at least one argument.
- All of your functions must return data or another function.
- No loops!
First, data in functional programs should be immutable, which sounds serious but just means that it should never change.
Secondly, functional programs should be stateless, which basically means they should perform every task as if for the first time, with no knowledge of what may or may not have happened earlier in the program’s execution (you might say that a stateless program is ignorant of the past).
Here is some interesting history about Functional Programming from: https://en.wikipedia.org/wiki/Functional_programming
Lambda calculus provides a theoretical framework for describing functions and their evaluation. Although it is a mathematical abstraction rather than a programming language, it forms the basis of almost all functional programming languages today.