Home > General Programming > General types of programming paradigms

General types of programming paradigms

February 25th, 2013 Leave a comment Go to comments

Motivation for writing

Right now I am working on an article related to delegates in C# and lambda internals. I wanted to explain their inner workings and the general motivation behind them, but I quickly realized that in order to do that, I should first provide some insights on the functional programming background, the difference between imperative and declarative programming, the concept of clojures, etc. I soon realized that it might be best if I write a separate article on that topic, mainly due to the fact it could quickly become overwhelming.

The information related to this topic on the internet is quite fragmented, which makes it difficult for assimilation. In the sections below I’ll try to describe concepts like Structural, Imperative, Declarative, Procedural, Object-Oriented, Functional and Domain-Specific programming, which are few of the basic types of programming paradigms.

Before you continue reading, please note that the following text is my own interpretation of these terms (pretty much like everything else on this blog), based on my experience and my knowledge. Like I said, the information on the topic in the internet is very fragmented and frankly – quite often contradictory.

The article is a preface to LINQ internals (Part 1): Delegates and events.

What is structured programming ?

Structured programming (also known as modular programming) is an old term referring to programming languages that support structured flow-control. For example, languages that support flow-control statements, functions/procedures, et cetera. As the name suggests, the Structured programming focuses on the separation of concerns, modularity and reusability.

All the modern languages are in fact structural, both imperative and declarative (see below). You might ask what is then not structural. Well, there are some old languages that don’t support any structural syntax– like the Assembly language, for example, where the only way to control the flow is the GOTO operator, which is clearly not a structured way. (although some assembly dialects do contain basic control-flow instruments).


Imperative and Declarative programming

Imperative programming is the programming style languages like C, C++, Java and C# use. It is based on specific operations described through statements – consequent commands and actions. The Procedural and Object-Oriented programming paradigms are all derivatives of the imperative style.

Declarative programming focuses on what should be achieved, rather than how exactly. The concrete command flow is not being specified, more or less, the focus is on the result. The Domain Specific languages and the Functional programming languages are both declarative.


Procedural programming

Procedural programming is an imperative-based type of programming, which is sometimes used as a synonym of imperative programming itself. It is build around the idea of building procedures and functions (routines). Although functions are also presented in the Functional Programming, the procedural style usually refers to the imperative paradigm. Languages like C and C++ are procedural.


Difference between a procedure and a function

Strictly speaking, there is a difference between a procedure and a function. In Pascal, for example, the procedure does not return any value, whereas the function returns.

In other languages like T/SQL and PL/SQL there are more differences. For example, the function is supposed to return only one value, it can also be called from a SELECT statement. Procedures usually change state, they have more business logic inside, they can call functions (which is not possible the other way around) and they can return multiple results.

In languages like C, however, the terms are used interchangeably. In Object-Oriented programming functions/procedures are usually called from a class and are therefore called methods, although you can also define standalone functions in C++.

Object-Oriented programming

Languages like C, C++, Java and JavaScript are procedural. But C++ and Java are also Object-Oriented.
The OOP paradigm is quite popular today, floating around the idea of classes (templates) and concrete instances representing objects from the real world and the business domain rather than from machine point of view. Encapsulation, polymorphism and inheritance are the top 3 properties that characterize the object-oriented programming style.


Object-oriented languages are different from languages with objects

Some languages do have objects, but are not object-oriented. For example, JavaScript possesses objects, but does not have the concept of classes, inheritance and polymorphism. Despite that, with some work JavaScript can emulate encapsulation (e.g. through the use of the Module pattern) and inheritance (e.g. through the use of prototypes), even polymorphism through some… techniques. I do believe, however, that the power of JavaScript is exactly in its simplicity and dynamic nature and I’d think twice before taking the time to work around this. 


Domain Specific Languages (DSL)

Ever heard of the Domain Specific Languages ? Well, you have definitely used one.

These are small declarative languages designed with a specific purpose in mind. For example, HTML is a declarative language designed to describe the structure of a web page. CSS is a declarative language designed to describe the appearance of the web page. SQL is also a declarative language, developed to describe query data from relational databases. Another example are regular expressions, and they are all declarative because you get what you specify, and it specific.


Functional programming

The Functional programming paradigm is a subtype of the declarative style and is used in languages like Clojure, Haskell and Erlang. Programs written in functional languages are executed by evaluating expressions, rather than statements that change some state (for example, the state of a variable). That’s why in purely functional languages, the variables don’t hold mutable values. They hold expressions and might point to functions, which are very similar to functions in mathematics (they got only one return value and are deterministic). Although Functional programming is also Structured, some of the control-flow constructs are not presented. For example, in pure functional languages there are no loop statements; the only way to iterate is through recursion.

Another main characteristic is that functions are treated like “first-class citizens”, basically meaning that there is almost no difference between the usage of data and functions. Functions are treated like data and functions are data.


Purely functional languages

Languages like Closure, Haskell and Erlang are purely functional. They don’t have any side effects, meaning that they are strongly deterministic. They don’t have the concept of a variable which state can be changed and effect the later executions of the same function. Purely functional languages do not allow any of the imperative style of coding (or at least, theoretically).

Imperative languages that support functional programming

There are languages that support some of the concepts in the functional programming, like treating functions as data (that can be passed to other functions, for example). This allows a very interesting hybrid type of programming, that is imperative at its core but allows the use of benefits specific to a functional language. Examples of such languages are JavaScript, Lisp, C# (in the newer versions of .Net) and even C++ and C through a limited support.

Some of the languages really support functional programming to an extent, although the syntax of some is better suited that the syntax of others for such kind of programming. C++ can support this with pointers to functions and extensive use of macros, but it’s really not suited for that. JavaScript’s dynamic nature really facilitates this kind of programming and even libraries like JQuery make extensive use of anonymous functions. C# also supports functional programming with the help of delegates, which are basically pointers to functions (check my article related to Delegates and Events) but provide a lot of extra functionality, including asynchronous support, Lambda functions and LINQ which heavily use them.


What is an anonymous function ?

An anonymous function is simply a function which does not have an explicit declaration, you declare the body of without specifying a name. Lambdas in C# are anonymous functions. For example :

and also

An interesting fact is that in Java there are no anonymous functions, instead anonymous classes are used. Well, you simply define an anonymous class with a method inside, which is quite ingenious. Just like that:

After that you can pass that object around as function parameter. If Processor is an interface, an anonymous object that implements that interface will be created.


And what is a closure ?

A closure is not a concrete element and it is not a synonym to an anonymous function or lambda expression. A closure is a behaviour that allows you to define an anonymous function which refers to a local variable outside that function, and not worry about that variable’s lifespan, even if the whole surrounding object is garbage collected.

The following JavaScript snippet illustrates a closure : 

In this case, you should not be troubled if the local variable will go out of scope and pushed out of the stack – that will not happen thanks to the closure mechanism.


Well, that was my humble attempt to summarize some of the well-known programming styles. Hope you enjoyed it. ;)

About the author:
Kosta Hristov (34 Posts)


Like the article ? Share it ! ;)

  1. Aditya
    May 22nd, 2013 at 14:21 | #1

    There are a lot more paradigms but think these are the best. Here is a good list of all http://en.wikipedia.org/wiki/Programming_paradigm

    Thanks for the post :)

  2. June 30th, 2013 at 01:48 | #2

    This is a good companion to other explanations of the different programming paradigms. Very useful for junior programmers to understand the differences between the languages.

  1. No trackbacks yet.

Current month ye@r day *

Audi R8 wallpapers Sony Xperia Z4 Tablet WiFi