Functional Programming is a very vast area, but fortunately, we can define it with simple key points, because it is derived from a key idea or a principle which is very easy, and here in this article we will cover that principle so you could get a better understanding of what is functional programming.
What is Functional Programming?
In computer science we do programming and in programming, we have different types of paradigm, such as Functional, Procedural and Object-Oriented programming.
A programming paradigm can be defined as the approach to solving a problem using programming or coding. The Origin of functional programming is derived from Mathematic, as we use different equations and functions in math, we have adopted the same concept and used in programming languages to solve problems. These days each high-level programming languages support functional programming because it reduces the redundancy of code and increases the concept of reusability of code.
Many times, we confused the Procedural programming with Functional programming, but both are different and the main disparity is, the outcome of the same input. As we know that Functional Programming is completely adopted from Mathematic and like mathematics no matter what if the input is same for a function the output would same too, but in Procedural programming the output of the input depends upon the state of the programming, so here in Procedural programming the output of the same input may differ from state to state.
Characteristics of Functional Programming
- Like a mathematical function, mapping of one input to one output similar in functional programming there is only one output for 1 input.
- The functional programming is a “without variable assignment programming”
- Data is immutable in functional programming which means if we pass x as 12 the value of x would remain 12 for the complete function.
- As data is immutable so we cannot perform a loop and other types of increment operation on data, so to perform a repetitive task we can use Recursion.
- It completely follows the concepts of Mathematics.
Terms of Functional Programming
There are some specific concepts and terminology we use in Functional Programming.
- First-Class and Higher-Order Functions
- Pure Functions
- Referential Transparency
First-Class and high-order functions
In high order function, the function can accept and return a function as input and output respectively. When we perform the differentiation d/dx, we perform it on function and the result is also in the form of a function for most of the cases. Decorators in programming languages are the perfect example of High order functions.
def fun(a): return a() def g(): print("something") fun(g)
With pure function, the output will remain the same for the same inputs no matter how many times you execute the function with the same input the result and outcome of the function would remain the same. This defines the immutability of the function input with its corresponding output.
There is another property of pure function which defines that, they do not have any side-effects on the memory or input-output.
When a function call itself in order to iterate over a statement is known as recursion. In functions, we often use recursion for iterative operation though it has an impact on memory because, for each recursion, it occupies the same amount of memory like a complete function.
The recursion of a function may be infinite so we set a base condition with each recursion function so when the recursion meets the base condition the program returns a value and terminate the function execution.
With modularity, we increase the productivity and reusability of code in the program. Small modules can be generated so we can use those whenever we want. As modules are created separately it helps us to test and debug each module with ease.
In the pure function, the outcome or result of a function remains constant with respect to the argument or input list, this is also known as Referential transparency. In referential transpiration, calling the same function with the same inputs return the dame result.
Advantages and Disadvantages of Functional Programming
- It makes code easy to test and debug.
- Achieve parallel processing.
- Provide the reusability of code with modularity
- Provide recursion for iteration.
- Increase the productivity of the developer.
- It adopts lazy evaluation which avoids repeated evaluation because the value is evaluated and stored only when it is needed.
- It has a complex definition.
- Decrease the readability of code.
- The recursion needs more memory and it can also slow down the performance of the function.
People are also reading: