Imperative Programming v/s Declarative Programming v/s Functional Programming

Posted by kaleidoscope on Geeks with Blogs See other posts from Geeks with Blogs or by kaleidoscope
Published on Fri, 14 May 2010 08:10:44 GMT Indexed on 2010/05/14 9:25 UTC
Read the original article Hit count: 863

Filed under:

Imperative Programming ::

Imperative programming is a programming paradigm that describes computation in terms of statements that change a program state. In much the same way as the imperative mood in natural languages expresses commands to take action, imperative programs define sequences of commands for the computer to perform.

The focus is on what steps the computer should take rather than what the computer will do (ex. C, C++, Java).

Declarative Programming ::

Declarative programming is a programming paradigm that expresses the logic of a computation without describing its control flow. It attempts to minimize or eliminate side effects by describing what the program should accomplish, rather than describing how to go about accomplishing it.

The focus is on what the computer should do rather than how it should do it (ex. SQL).

A  C# example of declarative v/s. imperative programming is LINQ.

With imperative programming, you tell the compiler what you want to happen, step by step.

For example, let's start with this collection, and choose the odd numbers:

List<int> collection = new List<int> { 1, 2, 3, 4, 5 };
With imperative programming, we'd step through this, and decide what we want:

List<int> results = new List<int>();
foreach(var num in collection)
{
    if (num % 2 != 0)
          results.Add(num);
}
Here’s what we are doing:

*Create a result collection
*Step through each number in the collection
*Check the number, if it's odd, add it to the results

With declarative programming, on the other hand, we write the code that describes what you want, but not necessarily how to get it

var results = collection.Where( num => num % 2 != 0);

Here, we're saying "Give us everything where it's odd", not "Step through the collection. Check this item, if it's odd, add it to a result collection."

Functional Programming ::

Functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. It emphasizes the application of functions.Functional programming has its roots in the lambda calculus.

It is a subset of declarative languages that has heavy focus on recursion.

Functional programming can be a mind-bender, which is one reason why Lisp, Scheme, and Haskell have never really surpassed C, C++, Java and COBOL in commercial popularity. But there are benefits to the functional way. For one, if you can get the logic correct, functional programming requires orders of magnitude less code than imperative programming. That means fewer points of failure, less code to test, and a more productive (and, many would say, happier) programming life. As systems get bigger, this has become more and more important.

To know more :

http://stackoverflow.com/questions/602444/what-is-functional-declarative-and-imperative-programming

http://msdn.microsoft.com/en-us/library/bb669144.aspx

http://en.wikipedia.org/wiki/Imperative_programming

 

© Geeks with Blogs or respective owner