What Are the Advantages of Functional Programming Over Imperative Programming?

Sameer HumayunComputer ScienceLeave a Comment

What actually is functional programming?

Functional programming is a programming paradigm whereby applications are written using blocks of self-contained code that performs a single action and is reusable. These are called functions.

This paradigm was created to support a pure functional programming approach to problem-solving. Functional programs can use functions in much more elaborate ways. In this method of programming, functions are treated just like objects like integers and are passed to other functions as arguments and returned as results. Rather than sequencing and looping, functional languages use recursive functions, i.e. functions that are defined in terms of themselves. Recursions are function which calls or repeat itself.

Some examples of pure functional programming languages are Lisp, Scheme and Haskell.

Advantages of functional programming

Functional programming has many advantages, such as:

  • Functional program code is bug-free or less error-prone code because the functional program is stateless and there are no side effects. The advantage of this is that the function doesn’t then change anything outside the function. The result is then that if we don’t call a function it won’t affect any other functions.
  • The state of a functional program doesn’t change i.e. it doesn’t have the option to be mutable. Pure functions are stateless, and a code is stateless when the outcome of that function isn’t changed by any of the preceding code. A segment of code is stateless when its outcomes aren’t influenced by previous events i.e. a function will always return same results for a given input parameter.
  • Functional program codes are units whereby each unit can run concurrently which effectively makes for a more efficient program.
  • Functional programming supports nested functions - you can write functions within a function.
  • If you need to evaluate a function you evaluate it once and save the answer/result and use the answer/result as many times as you want. That means you don’t have to run the same code again and again you only run it once. Some of the examples are Lazy Lists, Lazy Maps, etc.
  • If there are two pure functions or expressions which don’t have any data dependency the result is that they can be run in parallel and is therefore thread safe.

So, what is imperative programming?

The foremost used programming paradigm is imperative programming in which a program state is changed by using statements. Many of the most favoured programming languages which use the imperative style programming are Java, Python, Ruby, Assembly, JavaScript, and C. An imperative program is a sequence of instructions. In this method the execution of every operation can alter the memory state. Imperative programming is directly inspired by assembly programming.

There are various types of imperative programming. For example:

  • Machine and Assembly languages i.e. Low level imperative language.
  • Procedural languages i.e. High level imperative language (C, FORTRAN).
  • Structural & modular languages (BASIC, Pascal, COBOL).
  • Object oriented languages (C++ , JAVA, Perl, Ruby, Visual C++).
  • Event Driven programming languages 6. Object Based Languages (Visual Basic & PHP).

A downside to the stateless program is that functional programming code needs greater memory space. New objects have to be created each time to perform some actions. Functional programming is usually used and is good to use where there are several operations that need to be done on identical datasets. Functional programming has been described as a powerful and expressive programming paradigm, but it has never been as popular as imperative programming.

Functional vs imperative programming: a breakdown

Functional ProgrammingImperative Programming
Stateless States exists
Data is immutableData is mutable
Good for Parallel or Concurrent ProgrammingNot the best approach for Parallel or Concurrent Programming
Works better with Big Data processing and analysisNot the best approach for Big Data processing and analysis
Primary Manipulation Unit is “Function”Primary Manipulation Unit is Objects
It uses “Recursion” to iterateIt uses “Loop” to iterate Data
Order of execution is not importantThe order of execution is important

About the Author

A picture of Sameer Humayun the author of this blog

Sameer Humayun

Software Developer

Sameer is a software developer at Optima Systems. He studied BSC Computer Science at university and recently completed his studies. After graduating from university, he started at Optima Systems.

More Posts

Other Posts