Difference Between Functional Programming and Imperative Programming
Table of Contents
Key Difference – Functional Programming vs Imperative Programming
The key difference between functional programming and imperative programming is that the functional programming considers the computations as mathematical functions and avoids changing state and mutable data while imperative programming uses the statements that change the programs state.
A programming paradigm provides a style of building the structure and elements of a computer program. The programming paradigms help to classify programming languages based on their features. A programming language might influence more paradigms. In object-oriented paradigm, the program is structured using objects, and the objects pass messages using methods. The logic programming can express computation in exclusively in terms of mathematical logic. Another two programming paradigms are functional programming and imperative programming. Functional programming allows expressing computations as the evaluation of mathematical functions. Imperative programming provides statements that explicitly change the state of the memory. This article discusses the difference between functional programming and imperative programming.
CONTENTS
1. Overview and Key Difference
2. What is Functional Programming
3. What is Imperative Programming
4. Similarities Between Functional Programming and Imperative Programming
5. Side by Side Comparison – Functional Programming vs Imperative Programming in Tabular Form
6. Summary
What is Functional Programming?
Functional Programming is based on Mathematics. The key principle behind functional programming is that all the computation is considered as a combination of separate mathematical functions. A mathematical function maps inputs to outputs. Assume that there is a function called f(x)= x*x. The x value 1 is mapped to output 1. The x value 2 is mapped to output 4. The x value 3 is mapped to output 9 and so on.
Figure 01: Example of Functional programming language – Haskell
In functional programming, the patterns are considered. The Functional programming languages Haskell, uses the below method to find the summation of the numbers.
The sum function has integer values, and the result will also be an integer. It can be written as sum: [int] -> int. The summation can be done by following the below patterns.
sum[n] = n, the sum of one number is the number itself.
If there is a list of numbers, it can be written as follows. The n represents the first number, and ns represents the other numbers
sum (n, ns) = n + sum ns.
Above patterns can be applied to find the summation of three numbers which are 3,4,5.
3 + sum [4,5]
3 + (4 + sum [5])
3+ 4 + 5 = 12
A function or an expression is said to have side effects if it modifies some state outside its scope or has an observable interaction with its calling functions besides the returning value. Functional programming minimizes this side effects. The state changes do not depend on the function inputs. It is useful when understanding the behaviour of the program. One drawback of functional programming is that learning functional programming is harder compared to imperative programming.
What is Imperative Programming?
Imperative programming is a programming paradigm that uses the statements that change a program’s state. It focuses on describing how a program operates. Programming languages such as Java, C and C# are imperative programming languages. It provides a step by step procedure on what to do. Imperative programming languages contain structures such as if, else, while, for loops, classes, objects and functions.
Figure 02: Example of Imperative programming language – Java
The summation of ten numbers can be found in Java as follows. In each iteration, the i value is added to the sum and assigned to the sum variable. In each iteration, the sum value keeps on adding to the previously calculated sum.
int sum = 0;
for (int i =0; i< =10; i++) {
sum = sum + i;
}
Imperative programming is easy to learn, understand and debug. It is easy to find the program state because of using state variables. Some drawbacks are it can make the code lengthy and can also minimize the scalability.
What is the Similarity Between Functional Programming and Imperative Programming?
- Both Functional Programming and Imperative Programming are programming paradigms.
What is the Difference Between Functional Programming and Imperative Programming?
Functional vs Imperative Programming | |
Functional Programming is a programming paradigm that considers computation as the evaluation of mathematical functions and avoids changing state and mutable data. | Imperative Programming is a programming paradigm that uses statements, that change a program’s state. |
Structures | |
Functional Programming contains function calls and higher-order functions. | Imperative Programming contains if, else, while, for loops, functions, classes and objects. |
Programming Languages | |
Scala, Haskell and Lisp are functional programming languages. | C, C++, Java are imperative programming languages. |
Focus | |
Functional Programming focuses on the end result. | Imperative Programming focuses on describing how a program operates. |
Simplicity | |
Functional programming is hard. | Imperative programming is easier. |
Summary – Functional Programming vs Imperative Programming
A programming paradigm provides a style of building the structure and elements of a computer program. Functional Programming and Imperative Programming are two of them. The difference between functional programming and imperative programming is that functional programming considers the computations as mathematical functions and avoids changing state and mutable data while imperative programming uses the statements that change the programs state.
Reference:
1.“Imperative Programming.” Wikipedia, Wikimedia Foundation, 28 Mar. 2018. Available here
2.“Functional Programming.” Wikipedia, Wikimedia Foundation, 28 Mar. 2018. Available here
3.Computerphile. YouTube, YouTube, 30 Aug. 2013. Available here
Image Courtesy:
1.’Haskell Logo’By Haskell1965 – Own work, (CC BY-SA 3.0) via Commons Wikimedia
2.’14485179234’by Linux Screenshots (CC BY 2.0) via Flickr
ncG1vNJzZmivp6x7pbXFn5yrnZ6YsqOx07CcnqZemLyue8OinZ%2Bdopq7pLGMm5ytr5Wau26y1KearaGfo66tec%2BrpqCqkaK6qrrGZpinnF2rwG61zKmcq5mknsOmec%2BrpqCqkaK6qrrGaA%3D%3D