Functional programming is based on Lambda Calculus, a computational model proposed by Alonzo Church in the 1930’s. This paradigm falls under the declarative umbrella rather than imperative, meaning, programming is done with expressions or declarations instead of statements. (Relax… See below the properties and you will understand what this means)
Note: We can do programming in a functional style in languages that are not specifically designed for functional programming. C++, Java, Python etc. all added constructs to facilitate the functional style. So, programmers using “mostly imperative” languages may have utilized some of the functional concepts.
Pros: more concise, more predictable, and easier to test than imperative or object-oriented code only if you understand.
Main Concepts of Functional Programming:
- Pure Functions: A pure function is a function which follows given properties like
- For the same input, it always returns the same output.
- Has no side effects(For details check the link).
- Referential Transparency.
Although, these properties are somehow correlated. Like, a function can only return the same output for the same input, only if it has no side effects. Let’s see by examplesPython12def square(x):return x*x
The square function always gives the same output for the same x .Python12def random_num():return np.random.randn()
Even though we didn’t pass any arguments to the random_num function, it gives different output, meaning that random_num is not pure.
Now, let’s look at the side effect property.
Side effect is any state(value of the variable) change outside the called function scope. In simple terms, if a function changes the state/value of the variable that is outside function scope or local environment then the function/unit of code is said to have a side effect.Python12345678910111213x=3def square(x):x=x*xreturn xdef square_side():global xx=x*xreturn xprint(x) # 3print(square(x)) # 9print(x) # 3print(square_side()) # 9print(x) # 9
Clearly, the x value changes after square_side() function which means that this function has side effects. The x in the square() function is local to its scope so no change in outside x. Global in square_side() makes x global so any change in x will change it in whole code (outside function scope) so the square_side function is not pure(has side effects).
Another important property of pure functions is Referential Transparency means we can replace a function call with its resulting value without changing the meaning of the program. Let’s see by an examplePython123def double(x):return x*2y = double(2) # or y = 4
In the above example, we can easily replace double(2) by 4 because we know that this function always gives the same result for the same input(No side effects). This is Referential Transparency.
- First Class functions: “first-class” is a computer science term that describes something that has no restriction on its use. Thus, first-class functions mean it can appear anywhere in the program like as arguments to other functions and as their return values.
- Recursion: Recursion means letting an operation be repeated until it reaches the base case. In the functional paradigm, we do not use the loops and all the work is done by recursion.
Python12345def factorial(n):if n == 1:return nelse:return n*factorial(n-1)
- Avoid Shared State: A shared state is a state that is shared between more than one function or more than one data structure and you should avoid creating functions that rely on the same variable.
Now, you might have got some feeling about the Functional programming. Hope you enjoy reading.
If you have any doubt/suggestion please feel free to ask and I will do my best to help or improve myself. Good-bye until next time.