Click here to Skip to main content
15,881,424 members
Articles / Programming Languages / C++

Functional Vs Imperative Language Paradigms

Rate me:
Please Sign up or sign in to vote.
4.58/5 (10 votes)
17 Dec 2014CPOL2 min read 29.1K   6   9
Functional vs. imperative language paradigms

These days, I am deeply in love with Microsoft’s F# language and Apple’s Swift language. F# on one side has picked the good parts from Functional languages like ML and oCaml + Imperative languages / Object-Oriented language like C#.

Swift on the other end is a multi-paradigm languages picking language ideas from Objective-C, Rust, Haskell, Ruby, Python, C#, CLU, and far too many others to list.

Versus

While learning these languages, one question (in fact multiple questions) always comes to my mind:

  • “What is a functional language and what is imperative language? and how are they different ”

Here are few answers which I read/ got from books and on the web.

Introduction

The functional programming paradigm was explicitly created to support a pure functional approach to problem solving. 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.

With an imperative approach, 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.

Differences

Characteristic Imperative Approach Functional Approach
Programmer focus How to perform tasks (algorithms) and how to track changes in state What information is desired and what transformations are required
State changes Important Non-existent
Order of execution Important Low importance
Primary flow control Loops, conditionals, and function (method) calls Function calls, including recursion
Primary manipulation unit Instances of structures or classes Functions as first-class objects and data collections

Although most languages were designed to support a specific programming paradigm, many general languages are flexible enough to support multiple paradigms. For example, most languages that contain function pointers can be used to credibly support functional programming. Furthermore, in C# 3.0 and Visual Basic 9.0, explicit language extensions have been added to support functional programming, including lambda expressions and type inference. LINQ technology is a form of declarative, functional programming.

Summarize

Let’s generalize and say that there are two ways in which we can write code: imperative and declarative.

We could define the difference as follows:

  • Imperative programming: Telling the “machine” how to do something, and as a result what you want to happen will happen.
  • Declarative programming: Telling the “machine”1 what you would like to happen, and let the computer figure out how to do it.

1 Computer/database/programming language/etc

Courtesy: MSDN

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
QuestionMy vote of 4 Pin
pates30-Dec-14 10:34
pates30-Dec-14 10:34 
GeneralRe: My vote of 4 Pin
Vidyasagar Machupalli30-Dec-14 15:55
professionalVidyasagar Machupalli30-Dec-14 15:55 
QuestionGood Starting Pin
Suchi Banerjee, Pune22-Dec-14 1:46
Suchi Banerjee, Pune22-Dec-14 1:46 
GeneralRe: Good Starting Pin
Vidyasagar Machupalli22-Dec-14 3:53
professionalVidyasagar Machupalli22-Dec-14 3:53 
GeneralMy vote of 1 Pin
firstvic21-Dec-14 6:29
professionalfirstvic21-Dec-14 6:29 
GeneralRe: My vote of 1 Pin
Vidyasagar Machupalli22-Dec-14 0:51
professionalVidyasagar Machupalli22-Dec-14 0:51 
GeneralMy vote of 1 Pin
Mihai Agape21-Dec-14 0:22
Mihai Agape21-Dec-14 0:22 
GeneralMy vote of 5! Pin
jediYL19-Dec-14 18:51
professionaljediYL19-Dec-14 18:51 
It's too funny that the 'functional' language is depicted as the ""liberation of all languages! We shouldn't have to bother with coding any more than imagine the product that we want and two cups of coffee : one for you and one for the computer.
In my opinion an excellent programming language is, 1} an interesting idiom that makes it easier to remain attentive, 2} from which the problematic has been removed, 3} to work nearly parallel to the software solutions that developers are discovering. What more or less can evolution provide?
GeneralMy vote of 1 Pin
Icebeat19-Dec-14 3:09
Icebeat19-Dec-14 3:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.