Functions and Functional Programming in Python

Ayodabo oluwatomisin
6 min readOct 28, 2020

--

As a programmer, you must have heard of or used functions at some points in your programming journey. There is no demeaning the fact that functions are part of the essentials when organizing and building our codes.

Functional Programming has been around since a long time ago, but its usage over the years has been a concern as maintenance and scaling becomes increasingly difficult.

What are functions?

A function is a set of instructions that accepts inputs and produces output in finite time(university definition haha). A function must have an input, must execute at constant time per line, must have an output, must be finite, it can’t run forever.

Functions serve as great way to isolate code and prevent interactions between global variables. You must wonder, what is a global variable? these are variables that reside at the top level scope of our python script.

Functions in Python

Python provides many ways of implementing functions and using them. Also, there are numerous functions that have been implemented for which a programmer can use without fear.

In Python, there are two types of functions, in-built functions and custom functions. The former has been implemented already and they come with the interpreter, while the latter is created by the programmer.

Furthermore, there is another two types of functions, pure and impure functions with the inclusion of higher order functions. Pure functions are the ones that have nothing to do with the outside scope as they take in inputs, manipulate them and gives out output. Pure functions takes no outside variable and produces no side effects, the first example is a type of pure functions.

Impure funtions on the other hands, interact with the outside scope and produces side effects which may cause problems later in our code

There are numerous in-built functions like the print, map, input, open, filter functions, while custom functions are created with the def keyword.

The def keyword allows a programmer to initialize a function block, an example below

The above is a simple python function that takes in x as an argument and returns the square of x. This is a simple demonstration of how python functions work.

Functions in python can take any arbitary number of arguments and also provide keyword arguments which have pre defined value(s).

def func(arg, key=value): pass

In the above code snippet, key is an argument that a pre defined value value, while arg is called a positional argument. I will talk more on that later.

In python, everything is an object, including functions, thus you can pass them as arguments to functions and store them as variables.

You must have heard of anonymous functions right? well it is possible in python as well. Python provide a way of writing function expressions that can be assigned to a variable and can work like a normal function. We call them Lambda expressions. Lambda expressions are created with the keyword lambda. Let’s use the same example as before but this time, using the lambda expression

lambda function

The above is just like the first example but it doesn’t have the def keyword. Like I said earlier, you can assign the above expression to a variable and use it like a normal function.

However, the drawback of this method is that it doesn’t really support readability and it is hard to debug if there are thousands of lines of code.

FUNCTIONAL PROGRAMMING IN PYTHON

Functional programming is a paradigm where programs are separated into functions that takes in input and gives output to the user. This is a very interesting paradigm as it is simple to implement but sometimes hard to optimize and up-scale.

Python provides ways of using functional programming to organize our code by providing the implementations of functions that makes it possible. Functions like map, reduce and filter are functional programming tools and we will talk about them shortly.

Also, there is the concept of recursion, which eliminates the use of loops but can be very dangerous. I will tell you this, “if you have to use recursion, then don’t”. Recursion works in a way where a function calls itself repeatedly until it gets to a base case, where it is terminated. beware as the depth can be exceeded and it runs infinitely(you wouldn’t want that to happen to your PC).

recursion

Functional programming enforces the use of functions to implement most functionalities and eliminates the ide of classes.

Map, Reduce and Filter

First, we will tackle map. map is a function that takes in two arguments, first is a function, second is the iterable(s) and apply the function on the iterable(s) to return another iterable. It is like this, map(func, [iterable, iterable,…]). Iterable is any data structure that a loop can be called on, be it list, set, dictionary or tuple. the result of the map function is a map object that can be converted to an iterable. Below is an example

map

map applies square on every item in that list and store them in a map object which is then converted to a list and assigned to variable values.

Next is filter. It is similar in working to map but with a slight difference of the inclusion of condition. Filter creates an iterable from an existing one by applying the function on it and checking for the condition. it returns a filter object that can be converted to an iterable.

filter

For reduce, it is located in the functools module. just like the name, it is used to reduced an iterable to a single value by applying a function and storing it in an accumulator. Also, there is the chance of an initializer where you provide a starting value.

reduce

Applying the above snippet on an iterable will return a value based on the function provided. NB: the function must have two arguments for reduce to work

SUMMARY

Python is a multi paradigm language where you can implement various programming methods including OOP, Functional Programming, Procedural Programming and others.

We have been able to discuss what functions are and how they work in python including the different types of functions we have.

Also, lambda expressions otherwise known as anonymous functions or function expression, their implementation, usage and drawbacks.

Functional programming, which was our main focus, we’ve been able to define and talk about its usage, advantages and disadvantages.

Lastly, there is no constraint on the paradigm you are meant to use to build your program, what matters is you understand the method and you can easily scale it up and optimize it when the time comes.

Thank you for reading!

--

--

Ayodabo oluwatomisin

I am a Data Scientist and a Web Developer. I also write great articles!