blog

Home / DeveloperSection / Blogs / Imperative and declarative programming

Imperative and declarative programming

Vijay Shukla2847 25-Apr-2013

In this blog I am trying to explain the imperative and declarative (functional) programming.

Declarative (functional) programming:

Declarative (functional) programming expresses the logic of a computation without describing its control flow. Many languages applying this style attempt to minimize or eliminate side effects by describing what the program should accomplish, rather than describing how to go about accomplishing.

For instance, if you have a list in C# and you want to pull out every Nth element, you have to point at the first element, set a counter at one, move to the next element, increment the counter, check to see if you're at the Nth element and so on. The functional equivalent would be to write a function that recognizes when the size of a list is a multiple of N, and then pass that function to the list, possibly with another snippet of code to hand back the head of the list if your N-recognizer evaluates to true and discarding it if it evaluates to false. The two functions recurs through the list, and finally hand back a list consisting of every Nth element.

Imperative Programming

A developer writes code that describes in exacting detail the steps that the computer must take to accomplish the goal. This is sometimes referred to as algorithmic programming. In contrast, a functional approach involves composing the problem as a set of functions to be executed. You define carefully the input to each function, and what each function returns. The following table describes some of the general differences between these two approaches.


Updated 18-Sep-2014

Leave Comment

Comments

Liked By