Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C++

Functional Vs Imperative Language Paradigms

4.58/5 (10 votes)
17 Dec 2014CPOL2 min read 29.8K  
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)