Thursday 29 September 2016

Understanding the concepts of Functional Programming & Imperative Programming

Functional Programming

  • Functional programming is programming paradigm it's a style of coding in which most of the codes to be written in the from of functions.
  • In functional programming, we do everything with functions such as it takes something as input and gives an output. 
  • Functional programming is a form of declarative programming.
  • The functional programming paradigm was explicitly created to support a pure functional approach to problem solving.
  • Pure functional approach means a function that does not have side effects and also does not rely on the mutable state. In a simple word everything should reside inside the function such that when some change made to function it will have no side effects to the whole program. 
  • Easier testing and debugging. Because pure functions can more easily be tested in isolation, you can write test code that calls the pure function with typical values, valid edge cases, and invalid edge cases.
  • In functional approach, program flow control is based on Function calls, including recursion.

Advantages of Functional Programming

  • Bugs-Free Code
  • Efficient Parallel Programming
  • Better Performance
  • Better Encapsulation
  • Increase Reusability
  • Better Modularity
  • Increase Readability and Maintainability
  • Increase Testability
  • Robust & Reliable Code


Imperative Programming

  • Imperative Programming is one of the popular programming paradigms which executes a sequence of steps/instructions/statements in some order.
  •  In Imperative programming, you tell the computer what to do.Like "Computer , add x and y" or "Computer, do that do this" and the computer goes and does it.
  • An imperative language uses a sequence of statements to determine how to reach a certain goal. These statements are said to change the state of the program as each one is executed in turn.
  • In Imperative approach, primary flow control is based on Loops, conditionals, and function calls.
  • Examples of Imperative Programming are C, C++, JAVA etc.

Characteristics of Imperative Programming

  • Sequence of Statements.
  • Order of execution of Statements is very important.
  • They contain state.
  • They use both immutable and Mutable Data.
  • They can change state.
  • They may have Side-effects.
  • They directly change the state of Program.
  • They represent the state with Data Fields.

Example for both Functional and Imperative programming approach:


Imperative Approach:

A program to add a series of three numbers.

int total = 0;
int number1 = 5;
int number2 = 10;
int number3 = 15;
total = number1+number2+number3;

Functional Approach:

A program to add a series of three numbers.

int addSeries(int num1,int num2,int num3)
{
     int total = 0;
     int number1 = num1;
     int number2 = num2;
     int number3 = num3;
     total = number1+number2+number3;
     return total;
}

I hope by going through this simple example on functional and imperative programming approach will help you clear your doubts.

3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Thanks for the informative article! I have been using online courses websites for a while to boost my career opportunities and it really helped me alot.

    ReplyDelete