Can you help me understand the Functional Programming Paradigm, in brief?

View other answers to this thread

Learn Something New Everyday,
Connect With The Best Developers!

Sign Up Now!

& 500k+ others use Hashnode actively.

Raza Sayed's photo

Functional Programming is a fundamentally different way of thinking about programs than Object Oriented Programming. In OOP you model the world in terms of classes and objects. Its like how biologists would classify things based on taxonomy. Classes define the behaviour and Objects maintain state. Now because the state of the data that an object holds can be changed by other objects (mutable data) , you need to implement data hiding in OOP. Also, in the case when some piece of data needs to be accessed by multiple objects you need to implement synchronization techniques like locks, semaphores, monitors etc, else there is a risk of data corruption.

FP in contrast is a different way of looking at the world,there are no classes and objects, and instead of having mutable data as in OOP, you treat data as immutable, something that can never change, hence data can be accessed concurrently without any risk. Instead of classes and objects, FP models the world in terms of functions. Functions access data but never ever change it, given some input, they always return the same value and also don't have any side effects anywhere else like for example changing some other piece of data outside their scope etc. Hence functions in FP are known as 'pure functions'. For example consider the function below:

function add(x,y) {
   return  x + y;

Given some value of x and y , the add function will always return the same value, also it does not change anything else anywhere in the program, hence it is an example of a 'pure function'. In contrast, consider the functions below:

count = 0;

function increment(n) {
   count += n;   

function isEven() {
   return count % 2 == 0;

In the above example, the increment function changes the value of count which is outside of its own scope, and in the second example, the isEven function returns different values at different times, based on whether count is even or odd. Hence both are examples of impure functions.

Also, in addition to immutable data , functions in FP themselves are data and can be passed around as data. Hence functions in FP are referred to as 'first-class functions' . Also functions that accept other functions as input or return functions are known as 'higher order functions'. For example consider the function below:

function map(f, arr) {
      result = f(arr);
      return result;

I can now do this:

function add(arr) {
     sum = 0;
     for(i = 0 ; i < arr.length ; i++) {
         sum += arr[i];
     return sum;

function multiply(arr) {
     product = 1;
     for(i = 0 ; i < arr.length ; i++) {
         product *= arr[i];
     return product;


map(add,[1,8,10]); //Returns 19
map(multiply,[1,8,10]); //Returns 80 

You can see how i reuse the map function to do different things on the same array that i pass to it, based on what function i pass to it as the first parameter. The map function treats the function passed to it as data and can use it. Hence map is an example of a 'higher order function'.

Now you would ask why we need to bother about such things as immutable data and whats wrong with OOP. There is nothing wrong with OOP, but FP has certain advantages over it. For example, you know that computer programs are run by the processor in a computer. Computer processors have hit the limit of 'Moores Law' and have stopped getting any more faster. To counter this situation, hardware manufacturers now manufacture processors with multiple cores so that a processor can execute multiple programs/processes in parallel, in order to get more speed benefit from a single chip.

In order to take advantage of multi-core processors , as programmers , we need to write programs in such a way that they can easily be run in parallel on all cores available to us. Now, as i mentioned before when writing parallel programs there is a risk of data getting corrupted if multiple processes can access it at the same time. And here is where the immutability offered by FP comes into play. Since data is immutable we can easily parallelize our programs, taking advantage of multi-core processors and also don't have to worry about accidental modification of data. So to answer your question if you should learn about FP then i would say, definitely yes, as the world is moving to multi-core processors and parallel programming, so it would be good tool to have in your programming tool belt.

Coming to languages that support FP, i would like to make a distinction between languages that have a mix of OOP and FP and others that are purely functional. Languages like Javascript, Ruby, Python etc provide functional programming features but are not purely functional because they don't strictly enforce data immutability. It is possible to have higher order functions in these languages but it is also possible to have mutable data, also they support OOP concepts of classes and objects.

On the contrary languages like for example Haskell, Erlang, Elixir etc are purely functional languages because they don't allow mutable data. So based on your background and preferences you can take a pick.I would say in addition to learning how you can do FP in languages like say Javascript, Python etc it would be helpful to know atleast one purely functional language, as it would definitely make you a better programmer.

If want you want to get started with Haskell there is a great freely available online reference called 'Learn You a Haskell for great good ' ( . Coming from a Ruby background i have recently been looking into Elixir, and really enjoying it. Others can probably pitch in with their own suggestions. Hope this post has given you some insight into FP and all the best in your attempts at learning it. Have fun ! :).

Want to read more?

Browse featured discussions

© 2020 · Hashnode