In this tutorial, we will learn the basics of functional programming and will understand how to implement it in Python with some examples. We’ll also look at what are the advantages and disadvantages of functional programming.

## What is functional programming?

Functional programming is just another programming paradigm as procedural programming and object-oriented programming.

In functional programming, instead of writing statements to produce the output, we apply a sequence of functions to get the output.

It can be used in the cases where end result and intermediate or beginning values have no physical dependencies on each other.

For this, we decompose a problem into simple functions and use a sequence of single-purpose functions to perform a complex task.

## How to implement functional programming in python?

To implement functional programming in Python, we decompose the problem into pure functions and then we apply the functions in a declarative manner in a sequence to produce the output.

**By pure functions, we mean that the output of the functions should not depend on global variables and states of the program and it shouldn’t produce any side effect**.

That is, the output of functions used in functional programming should only depend on input.

In this article, we will work with `map()`

, `filter()`

and `reduce()`

methods to convert a procedural program to a functional program.

### 1. map() function in Pyhton

The `map()`

function takes a function as its first argument and an iterable object as its second argument or any number of iterables after that. Then it returns a map object after applying the function on each element of the input iterables. We can then convert the map object to the type of iterable we want.

#define the function that increments a number by 1 def increment_by_one(number): return number+1 #define a list numbers= [1,3,45,67,34,78,23,56,98,104,123] print("input list to increment values by 1 is:") print(numbers) #procedural way to get a list containing incremented elements by 1 result=[] for num in numbers: result.append(increment_by_one(num)) print("Result obtained through procedural way is:") print(result) #functional way to obtain a list containing incremented elements by 1 resultbyfunc=map(increment_by_one,numbers) print("Result obtained through functional way is:") print(list(resultbyfunc))

Output:

input list to increment values by 1 is: [1, 3, 45, 67, 34, 78, 23, 56, 98, 104, 123] Result obtained through procedural way is: [2, 4, 46, 68, 35, 79, 24, 57, 99, 105, 124] Result obtained through functional way is: [2, 4, 46, 68, 35, 79, 24, 57, 99, 105, 124]

### 2. filter() function in python

The `filter()`

function applies a function on an iterable that tests each element of the input iterable for a condition and returns true or false.

It takes a function as its first argument and other arguments are the iterables on which the input function has to be applied. After execution filter() also returns an iterator which iterates only those elements of input iterables that returned true when passed to input functions.

#define the function that returns true when passed an even number as input def check_if_even(number): if number%2==0: return True else: return False #define a list numbers= [1,3,45,67,34,78,23,56,98,104,123] print("input list to filter even numbers is:") print(numbers) #procedural way to get a list containing even numbers from input list result=[] for num in numbers: if check_if_even(num)==True: result.append(num) print("Result obtained through procedural way is:") print(result) #functional way to obtain a list containing even numbers from input list resultbyfunc=filter(check_if_even,numbers) print("Result obtained through functional way is:") print(list(resultbyfunc))

Output:

input list to filter even numbers is: [1, 3, 45, 67, 34, 78, 23, 56, 98, 104, 123] Result obtained through procedural way is: [34, 78, 56, 98, 104] Result obtained through functional way is: [34, 78, 56, 98, 104]

### 3. reduce() function in Python

The `reduce()`

method is used for generating cumulative values like the sum of all elements of an iterable. It is defined in `functools`

module.

We can pass a function that takes two arguments and returns a cumulative output as the first argument to reduce() and an iterable as the second argument.

`reduce()`

applies the input function to items of the input iterable from left to right and reduces the iterable to a single cumulative value and returns the value.

Here is an example which uses both procedural way and `reduce() `

function to find the sum of elements of a list.

#import reduce function from functools import reduce #define the function that returns the sum of two numbers when passed as input def add(num1,num2): return num1+num2 #define a list numbers= [1,3,45,67,34,78,23,56,98,104,123] print("input list to find sum of elements is:") print(numbers) #procedural way to get the sum of numbers from input list result=0 for num in numbers: result=result+num print("Result obtained through procedural way is:") print(result) #functional way to obtain the sum of numbers from input list resultbyfunc=reduce(add,numbers) print("Result obtained through functional way is:") print(resultbyfunc)

Output:

input list to find sum of elements is: [1, 3, 45, 67, 34, 78, 23, 56, 98, 104, 123] Result obtained through procedural way is: 632 Result obtained through functional way is: 632

Now we will see an example to understand how to use functional programming.

## Converting a procedural program to functional program

Suppose we are given a list of numbers and we have to **find the sum of squares of even numbers which are divisible by 5 in the list**.

We will use the procedural and functional paradigm to implement the solution to the question and will try to see the differences between the program.

**Following is the procedural way of implementing a solution to the above problem.**

#define a function that returns square of a number def square(num): return num*num #define a function that checks if a number is even def is_even(num): if num%2==0: return True else: return False #define a function that checks divisibility by 5 def is_divisible_by_five(num): if num%5==0: return True else: return False #define a list numbers= [1,20,45,67,34,78,80,23,56,98,104,50,60,90,123] print("input list to find the solution is:") print(numbers) #procedural way to find the solution #extract elements which are dvisible by 5 and are even temp=[] for num in numbers: if is_even(num) and is_divisible_by_five(num): temp.append(num) #calculate square of elements in temp sqtemp=[] for num in temp: sqtemp.append(square(num)) #find sum of squared elements result=0 for num in sqtemp: result=result+num print("Result obtained through procedural way is:") print(result)

Output

input list to find the solution is: [1, 20, 45, 67, 34, 78, 80, 23, 56, 98, 104, 50, 60, 90, 123] Result obtained through procedural way is: 21000

**Now we will implement the above code in the functional paradigm in the following way.**

#import reduce function from functools import reduce #define the function that returns sum of two numbers when passed as input def add(num1,num2): return num1+num2 #define a function that returns square of a number def square(num): return num*num #define a function that checks if a number is even def is_even(num): if num%2==0: return True else: return False #define a function that checks divisibility by 5 def is_divisible_by_five(num): if num%5==0: return True else: return False #define a list numbers= [1,20,45,67,34,78,80,23,56,98,104,50,60,90,123] print("input list to find the solution is:") print(numbers) #functional way to find the solution #filter numbers divisible by 5 temp1=filter(is_divisible_by_five,numbers) #filter even numbers temp2=filter(is_even,temp1) #find square of numbers temp3=map(square,temp2) #find sum of squares result=reduce(add,temp3) print("Result obtained through functional way is:") print(result)

Output:

input list to find the solution is: [1, 20, 45, 67, 34, 78, 80, 23, 56, 98, 104, 50, 60, 90, 123] Result obtained through functional way is: 21000

## Differences between procedural and functional programming

- In procedural programming, we use a sequence of instructions using conditional operators and loops to implement our example while we just made function calls by passing them data and passed the returned values to another function to obtain the result. No conditional operator was used in the implementation of the main logic.
- In functional programming, we use pure functions and they perform very simple operations as we have done in our example but functions in procedural programs may be very complex and can have side effects.
- As procedural programs involve conditions, they are hard to debug whereas functional programs are declarative and each function has a fixed work with no side effect, making them easy to debug.

## Advantages of functional programming

As seen in the above examples, the following are the advantages of functional programming:

- As we use pure functions in functional programming, Debugging becomes easy.
- The reusability of pure functions is high and it only completes a single operation in a call, so using pure functions increases the modularity of the program.
- The readability of functional programs is high because the program is declarative and there are no conditional statements.

## When should you use functional programming?

Functional programming is best for doing mathematical computations. If you are solving complex mathematical programs that can be divided into nuclear steps, functional programming is best choice for the situation.

## When you should not use functional programming?

- You should not use functional programming if you are a beginner at programming. Our mind is trained to understand sequences and initially, it is hard to understand even procedural programs.
- If you are working on a large project, avoid using functional programming because the maintenance of functional programs is difficult during the coding phase.
- Reusability of code is a very tricky task in functional programming, so you need to be really good at it to save your time and effort.

## Conclusion

In this tutorial, we have seen what functional programming is and how to implement it in python. We have also seen what are the differences between procedural and functional programming, what are the advantages of functional programming, and whether or not we should use functional programming for a given task.