Functional Programming — A declarative programming paradigm

Puja Deshmukh
3 min readOct 10, 2022

--

To avoid the spaghetti code created by procedural languages such as C, FORTRAN, and Pascal two new programming paradigms were introduced by the computer architects: Object Oriented Programming(OOP) and Functional Programming(FP). Both these paradigms have different philosophies about how the software code should be structured.

The basis of object-oriented programming is to group related functions and variables into objects. In OOP the code describes how the results should be achieved in a step-by-step process, this results in a change in the program state after the execution of each step. Hence Object Oriented programming is also called an imperative paradigm.

The four main pillars that help us in achieving object-oriented programming are encapsulation, inheritance, abstraction, and polymorphism, however, each of these pillars is dependent on the other. For example, encapsulation is a necessary feature for abstraction and inheritance to be effective. Polymorphism can not exist without inheritance. To sum it up, in OOP the state of the object is shared across the code which turns out to be a concern in case of the large and complex code base, also the execution time is more as compared to functional programming

Functional programming is centered around building software composed of functions, avoiding shared space and immutability. Functions are treated as primitive in this paradigm and hence they can be stored in a variable, passed as an argument, and returned from a function. Below are the key concepts of functional programming

Functional programming aims at writing pure functions whenever possible. Pure functions are the ones that do not change the state of the program and therefore are said to have minimum side effects.

Pure Function

The sum function in the MathUtility class is called as a pure function since the resultant value is only dependent on num1 and num2. If we call sum(1,2) we will always get value 3.

Using a higher order function instead of using iterative loops for and while. A higher-order function takes a function as an input, performs an operation on the input function, and returns a function. There are two styles of recursion, head recursion is where we hold the state of all previous calls until we reach the base case. Consider a function to calculate the factorial of a number:

Head Recursion

This way of recursion works well for a small dataset but proves to be inefficient for a larger number. The solution to that is to use tail recursion

Tail Recursion

Immutability is the permanence of a variable once the value is assigned to it. Functional programming provides immutable variables, i.e once a value is assigned to a variable it can not be modified. It is immutability that enables functional programming to use pure functions. These functions do not mutate their inputs and rely on nothing but the input to achieve results.

Functional Programming provides various advantages as :

  1. The basis of functional programming is to use pure functions, and these functions are easier to test. The outputs are always the same for any given input. This ensures determinism and predictability.
  2. Functional programming leads to fewer bugs since each pure function is simply a mapping of inputs to outputs, a simple stack trace or print statement at each level will reveal the problem. With imperative paradigms, you could have shared or mutable state almost anywhere else in the codebase that’s potentially causing the bug.
  3. Concurrency is more easily maintained since pure functions are definitionally thread-safe. Because pure functions never share state with other sections of a program they can’t have race conditions.
  4. Immutability makes programs much simpler and improves developer speed since every value in the program is only assigned once.

Pure Functional Programming languages are Haskell, Clojure, Scala, PureScript, and Elm. It is also supported by many other programming languages such as Python, Go, Java and JavaScript.

--

--

No responses yet