Functional Programming Paradigm
Introduction
A paradigm for creating computer programs using expressions & functions while maintaining the state or data is known as functional programming. Functional programming aims to develop code that is simpler to understand and more bug-resistant. This program depends on mathematical operations. It is a declarative programming approach. It focuses on "what to solve" instead of "how to solve."
Functional programming is utilized when we need to perform a variety of operations on the same collection of data. When functional programming is used to write the code, a compiler is able to memorize the outcomes, parallelize the instructions, and wait for evaluating results.
Instead of statements, it uses expressions. A statement is executed to assign variables, and an expression is evaluated to generate a value. The statement's execution order is not important in functional programming. An essential component of the functional paradigm is the lambda calculus framework. Alonzo Church created this framework in 1930.
Functional programming languages have two groups
1. Pure Functional Language
This functional language mainly supports functional paradigms.
2. Impure Functional Languages
This kind of functional language support both functional paradigms and imperative programming.
Functional Programming Concepts
Pure functions, recursion, referential transparency, first-class and higher-order functions, and immutable variables are the five concepts of functional programming that are described below in detail.
1. Pure Function
A function is said to as a "pure function" if it consistently produces the same outcome for the same argument values. They don't alter input or output streams, local or global variables, or arguments.
2. Referential Transparency
In functional programming, once the variable is defined, its value is not changed throughout the program. There is no assignment statement in functional programming. If the user needs to store a value, the user has to declare and define new variables. In this programming, the state of every variable is constant at all times.
3. Recursion
Functional languages use recursion to implement iteration. Recursive functions continuously call themselves until the particular condition is satisfied.
4. First-Class Functions and Higher-Order Functions
When functions are considered similar to other variables, they are known as first-class functions. Higher-order functions are the functions that take another function as an argument and return another function. Without the First-class function, higher-order functions are not possible.
5. Variables are Immutable
In functional programming, once a variable has been initialized, it cannot be changed. We can add new variables, but we cannot modify existing ones, which significantly helps in-state maintenance throughout a program.
Characteristics of Functional Programming
- This language is based on mathematical operations that utilize conditional expressions and recursion.
- The higher-order functions are supported by functional programming.
- There is no support for flow controls in this language.
- Lazy evaluation features are present in functional programming.
- Abstraction, encapsulation, inheritance, and polymorphism are all supported by functional programming languages.
Advantages of Functional Programming Paradigm
1. Bugs-Free Code
This programming doesn't support states that allow us to construct error-free codes.
2. Efficient Parallel Programming
This language doesn't support mutable states since state changes are not an issue here. It is possible to program "Functions" to run simultaneously as "instructions."
3. Lazy Evaluation
Functional programming is compatible with lazy functional constructs, such as lazy lists.
4. Efficiency
Functional programs are made up of separate parts that concurrently execute.
5. Supports Nested Functions
Nested Functions are supported by functional programming.
Disadvantage of the Functional Programming Paradigm
- A lot of memory is needed for functional programming.
- Users must create new objects to conduct actions because it does not have a state.
- Recursive programming is more challenging to write than loop-based programming.
- Immutable values and recursion decrease performance.
- Although writing pure functions is simple, integrating them with other applications and I/O activities is challenging.
Programming Languages that support functional programming
The first high-level functional programming language is LISP. Artificial intelligence applications, including language processing, voice and vision modelling, and ML, uses LISP. The other famous functional programming languages are Erlang, Haskell, Python, Scala, JavaScript, ML, OCaml, Clojure, Common Lisp, and Racket.
Applications of Functional Programming
- It is utilized when the user has to implement a parallelism mechanism.
- It is used in mathematical calculations.