5 minutes read

Functional programming is a programming paradigm that treats functions as first-class citizens. In functional programming, programs are constructed by composing and applying functions. Key properties of functional programming are:

A pure function, like a mathematical function, takes an input and returns a value without any intermediate manipulations. For example, sin(x)sin(x) simply returns a value. You give it a value of xx, and it returns the value the sine of it. If you give it the same value you will always get the same answer.

import math

def sine(x):  # Computes sine
    return math.sin(x)

Unlike pure functions, python functions can set a global variable that might influence the result of a different function when that is called. It might write something to disk, or send some data across the network. These influences are called side effects.

x = 5

def assign(num):
   global x
   x = num

print(x)
print(x+1)
assign(10)
print(x)
# 5
# 6
# 10

As you can see in the above code, the function instead of doing some computation and returning the output changes the value of the global variable xx. The change in this value is the side effect.

Python is prone to side effects and constructing functions that aren't pure, but certain constructs make functional programming possible. They are:

First-class functions

Everything's an object in Python. Every variable, every function or method, boolean values, and even the None value is an object. The following code demonstrates it:

a, b, c = 5, True, None

def sum(num1, num2):
   return num1 + num2

print(f"a is {type(a)}, b is {type(b)}, c is {type(c)}")
print(f"The sum method is of type {type(sum)}")
# a is <class 'int'>, b is <class 'bool'>, c is <class 'NoneType'>
# The sum method is of type <class 'function'>

As you can see, everything in Python is an object of a certain class.

Since functions are objects in Python, just like any variable, they are:

  • Created at runtime,

  • Assigned to a variable or element in a data structure,

  • Passed as an argument to a function,

  • Returned as the result of a function.

Functions being a first-class entity in Python allow us to create complex functions that can take other functions as input or return a function as output. These functions that either take a function as an input or return a function as output are called higher-order functions. Some common higher-order functions in Python are:

  • map();

  • filter();

  • reduce()

# Return twice of n
def addition(n):
    return 2 * n

numbers = (1, 2, 3, 4)
result = map(addition, numbers)  # map() takes ‘addition’, a function, as input

In the above code block, notice how the map() function takes addition, another function, as its input.

A more common example of a higher-order function is a recursive function. A recursive function calls itself in the body of the function and returns the function. The function below uses recursion to compute the sum of natural numbers up to nn.

def recursive_sum(n):
   if n <= 1:
       return n
   else:
       return n + recursive_sum(n-1)  # Returns a function

Immutable data types

Data types like tuple, namedtuple, str, and int, the values that we cannot modify, are called immutable data types. These data types play a central role in functional programming because they can't be modified and hence prevent side effects in a function.

Conclusion

  • Functional programming is a programming paradigm that treats functions as first-class citizens.

  • Functional programming uses pure functions and immutable data structures to avoid side effects

  • Functions in python are just like any other object. They can be passed as arguments to other functions and can be returned as the output of the function.

  • Immutable data types can't be modified.

26 learners liked this piece of theory. 1 didn't like it. What about you?
Report a typo