Functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions, avoiding changes in state and mutable data. The main idea of functional programming is “what to solve”.
As there are dozens of programming languages that are written in procedural, object-oriented, functional, scripting, etc. ways. So choosing a programming language based on its applications is mostly preferred.
Functional Programming Concept
In functional programming languages, functions are treated as First-Class Citizens means that we can pass functions as arguments in another function, assign a function to the variable, return one function from another function, and lots more that we will cover later in this post.
Functions here in functional programming should be pure functions that always should return the same value(deterministic).
Pure Function Properties:
- It does not access global state variables.
- It always returns the same result, given the same argument. So we say in example trigonometric functions sin(x), cos(x), tan(x), etc. are all pure functions.
- It does not have “side effects”.
- It cannot use the internal state to determine what result to return.
Those functions which are not pure functions are called impure functions. The impure function may return different values depending upon the input from the user.
Apart from the above criteria, in functional programming repetition must be done by recursion rather than by iteration.
Let us take a simple pure function example:
z = 7 def add(x, y): return x + y
Above we can see that the add function does not touch the z variable(global variable). It does not read from z and it does not write to z. It only reads x and y, its inputs, and returns the result of adding them together.
That’s what is called a pure function. But if the add function did access the variable z value and modify it, then it would be no longer a pure function(impure).
High Order Functions
As we already discussed that Python treats functions and methods as first-class citizens, so in Python, we can pass one function to another function by just treating one function as an object. Thus high order functions are those functions that take other functions as inputs and or outputs.
Passing one function to another
>>> def div(a, b): ... return a / b ... >>> def calc(i, j, div): ... return div(i, j) ... >>> div(10, 2) 5.0 >>> calc(10, 2, div) 5.0
In the above calc function to calculate the division of two numbers, we pass another function method div which performs division operation along with two arguments. In this way, in python, we can pass one function to another function.
Assigning function to the variable
>>> def div(a, b): ... return a / b ... >>> div(10, 2) 5.0 >>> div_var = div >>> div_var(10, 2) 5.0
Here we assigned a function called div to the variable div_var. So instead of using div, now we can use div_var(a, b).
Returning function from another function
>>> def div(a, b): ... return a / b ... >>> def calc(i, j): ... return div(i, j) ... >>> calc(10, 2) 5.0
In the function calc we return another function called div.
Advantages of Functional Programming
- Faster execution via memorization
- Compiler optimization
- Lazy evaluation