Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Functional Programming Vs Declarative Programming Vs Imperative Programming

I have been too used to Imperative Programming which is the usual way of telling the computer to perform step by step the procedure to get the final result. On the other hand, declarative programming just passes the input and expects the output without stating the procedure of how it is done. The one I am confused about is Functional Programming. I know Functional Programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data and is not a type of declarative language. However, I cannot still comprehend how it can work.

Let's take an example of executing the Fibonacci numbers.

Imperative Programming:

#include<stdio.h> 
#include<conio.h> 
main() 
{ 
  int n,i,c,a=0,b=1; 
  printf("Enter Fibonacci series of nth term : "); 
  scanf("%d",&n); 
  printf("%d %d ",a,b); 
  for(i=0;i<=(n-3);i++) 
  { 
    c=a+b; 
    a=b; 
    b=c;    
  } 
  printf("%d ",c);
  getch(); 
} 

Declarative Programming:

Give the nth number and it will return the value of the nth number

How does the Functional Program work?

Plus do correct me if my definitions are wrong. Please feel free to comment..

like image 798
lakshmen Avatar asked Jun 07 '12 04:06

lakshmen


People also ask

Is functional programming declarative or imperative?

Functional programming is a form of declarative programming. In contrast, most mainstream languages, including object-oriented programming (OOP) languages such as C#, Visual Basic, C++, and Java, were designed to primarily support imperative (procedural) programming.

What are the 3 levels of programming languages?

There are three types of programming languages: machine language, assembly language, and high-level language. Machine language is easier for the computer to understand but harder for the programmer to understand.

Is C++ declarative or imperative?

Fortran, Java, C, C++ programming languages are examples of imperative programming.

Is Python imperative or declarative?

As mentioned earlier, Python can be used in both imperative and declarative programming and is one of the most popular languages today for beginners and experts alike.


3 Answers

Your example of declarative programming above is not an actual program, so it's not a good example.

The main difference is between imperative and declarative. Functional is a particular kind of declarative.

C, C++, Java, Javascript, BASIC, Python, Ruby, and most other programming languages are imperative. As a rule, if it has explicit loops (for, while, repeat) that change variables with explicit assignment operations at each loop, then it's imperative.

SQL and XSLT are two well-known examples of declarative programming. Markup languages such as HTML and CSS are declarative too, although they are usually not powerful enough to describe arbitrary algorithms.

Here is an example computation (summing the income by gender, from a suitable data source) first written in an imperative language (Javascript) and then in a declarative language (SQL).

Imperative programming

var income_m = 0, income_f = 0;
for (var i = 0; i < income_list.length; i++) {
    if (income_list[i].gender == 'M')
        income_m += income_list[i].income;
    else
        income_f += income_list[i].income;
}

Notice:

  • explicit initialization of variables that will contain the running totals;
  • explicit loop over the data, modifying the control variable (i) and the running totals at each iteration;
  • conditionals (ifs) are only used to choose the code path at each iteration.

Declarative programming

select gender, sum(income)
from income_list
group by gender;

Notice:

  • memory cells to contain running totals are implied by the output you declare you want;
  • any loop the CPU will need to perform (eg. over the income_list table) is implied by the output you declare you want and by the structure of the source data;
  • conditionals (eg. case in SQL) are used in a functional way to specify the output value you want based on the input values, not to choose a code path.

Functional programming

As I mentioned above, SQL's case is a great example of the functional way of programming, which is a restricted subset of Declarative programming in which the desired computation is specified by composing functions.

Functions are things that accept inputs and return outputs (eg. case, sum()…)

Composition means chaining two or more together by specifying how the output of one is fed as the input to the next (typically by writing one inside the other.) Finally the whole composition, which is still itself a big function, is applied to the available inputs to get the desired output.

In this snippet I am declaring the output I want by composing the functions sum() and case. This is called functional programming:

select 
    sum(case when some_flag = 'X' then some_column
        else some_other_column end)
from
    ...

If the composition of two or more functions and their application to the input data are the only constructs available in a given language, that language is said to be purely functional. In those languages you will notice the complete absence of loops, variable assignment and other typically imperative statements.


Edit: I recommend watching some of Anjana Vakil's talks on functional programming in Javascript, to get a better idea of what it's about.

like image 180
Tobia Avatar answered Oct 16 '22 11:10

Tobia


It is an erroneous oversimplification to claim that imperative programming is distinguished from declarative programming by erroneously assuming a lack of ordering in the latter.

Pure functional programming is not prevented from expressing order and implementation, rather it is less able to express random accidental order at the operational semantics level. Also it has the advantage of "Don't repeat yourself" (DRY), which is a form of declarative style (see below).

However, pure functional programming does not guarantee declarative high-level semantics. For this, you need to apply the correct definition of declarative vs. imperative.

like image 5
Shelby Moore III Avatar answered Oct 16 '22 12:10

Shelby Moore III


Another useful explanation I found in the Pro XAML with C#:

On Declarative

In declarative programming, the source code is written in a way that expresses the desired outcome of the code with little or no emphasis on the actual implementation.

On Imperative

Imperative programming is the opposite of declarative programming. If declarative programming can be thought of as declaring what the desired outcome is, imperative programming can be viewed as writing lines of code that represent the instructions of how to achieve the desired outcome.

like image 4
Themelis Avatar answered Oct 16 '22 12:10

Themelis