Types of Programming Paradigms


Every day, we come across different programming languages that we use for specific purposes. A program can be built in multiple ways to provide solutions to a problem.

But the path we choose has a lot to do with the time and space complexity of the code, development time efficiency, and even how easily other coders can understand the code. Before getting deep into the subject, let us all understand what a programming paradigm means.

What Is a Programming Paradigm?

A programming paradigm is the process of writing code in an organized manner specified by the language. Each language is used as a tool for solving a problem and has its own unique style that uses a programming paradigm.

A program is like a machine. It has many components and parts to work efficiently. This seems great at the start, but this complexity can become your own enemy while translating to real-world problems to provide simple and elegant solutions.

There are different methods proposed by computer scientists that we can follow to create efficient code for our problem statement.

Now, let us look into the different types of programming paradigms available. They are broadly classified into Imperative Paradigm and Declarative Paradigm, which are further subclassified as shown in the following diagram.

Types of Programming Paradigms
Types of Programming Paradigms

In the modern programming world, most languages are multi-paradigm languages. A programming language can use the aspects of different paradigms so that we get maximum advantages from all paradigms.

For example, Python is a mixture of procedural, object-oriented, and functional programming paradigms. The language is usually coded using the imperative style but can use the declarative style if required.

Now let’s look at each programming paradigm in detail to understand how they work.

Imperative Programming

When you go shopping, you usually write down the name of the things you need to buy before reaching the mall. Similarly, this paradigm consists of a list of programming statements.

They are first executed, and then the results are stored in a variable. It is more of a line-by-line instruction given to the computer.

Let us understand imperative programming using a C++ program that gives us factorial of a number.

Example:

#include<iostream>
using namespace std;
int main(){
  int fact=1;
  fact*=1;
  fact*=2;
  fact*=3;
  fact*=4;
  fact*=5;
  fact*=6;
  fact*=7;
  fact*=8;
  cout<<"Factorial of 8 is : "<<fact;
  return 0;
}

Output:

In this example, we give a line-by-line task to perform by the computer with proper instructions. This type of paradigm takes the order of the steps into consideration because different arrangements can produce different results.

Imperative programming is further subclassified into Procedural Programming, Structured Programming, and Object-Oriented Programming.

Procedural Programming

This programming paradigm follows imperative programming with procedural calls. These calls direct the system to perform the required tasks.

Each procedure or so-called function can have multiple commands to be executed. The function, once defined, can be called as many times as needed to perform the same operation.

Procedural programming is considered the best for people starting to learn to code. Beginners can start with procedural programming as it is simple, efficient, requires less memory, and is easier to keep track of control flow.

Some of the languages that use procedural programming paradigms are:

  • C
  • C++
  • Java
  • Python
  • Pascal

Example program in C++:

#include<iostream>
using namespace std;
  int main(){
    int fact=1;
    for(int i=1;i<=8;i++)
    fact=fact*i;
    cout<<"Factorial of 8 is : "<<fact;
    return 0;
}

Output:

Structured Programming

It is an imperative programming paradigm where we use nested loops, conditional statements, and subroutines to define the control flow of a program.

This paradigm has a top-down implementation which promotes code readability and reusability. In commercial applications, this helps to reduce development time and increase code quality.

The disadvantage of structured programming is that it has reduced execution efficiency compared to the other paradigms. Structured programming languages are at risk of “over-structuring” and loss of efficiency.

Some of the languages that use Structured programming paradigms are:

  • Algol 60
  • PL/I
  • Pascal
  • Ada 83
  • Modula

Object-Oriented Programming

This is the most widely used and most popular programming paradigm. Class, Abstraction, Encapsulation, Inheritance, and Polymorphism form the backbone of Object-Oriented Programming (OOP).

In this paradigm, every entity in the program is considered an object. Objects can have data associated with them called attributes and actions they can perform using methods.

OOP featureDescription
AbstractionIt helps in hiding unnecessary attributes while showing the ones required.
InheritanceIt helps in establishing hierarchical relationships promoting code reusability.
PolymorphismIt allows different objects to respond in different ways to the same input.
EncapsulationIt is the binding of data into a single unit.
Table describing the features of object-oriented programming

Some of the languages that use object-oriented programming paradigms are:

  • Python
  • Ruby
  • Java
  • C++
  • Smalltalk

I have written a separate article on Objected Oriented Programming in Python. Do check it out if you are interested.

Declarative Programming

Declarative programming is a programming approach that expresses a computation’s logic without discussing its control flow. The programmer must specify what the program must accomplish but need not specify how it must be implemented.

In other words, rather than educating on “how to reach a goal”, declarative programming concentrates on “what needs to be accomplished first”.

The primary distinction is that imperative commands tell you how to do, while declarative commands tell you what to do. The declarative programming paradigm is further divided into logic programming and functional programming.

Logic Programming

The logic programming paradigm uses logic-based statements to convey facts and rules. Logical inferences are made of instructions or logic statements to do computation. Atomic statements are built using these predicate statements.

While displaying relevant data, logic languages frequently rely on queries. Some of the specific uses of logic programming are natural language processing, database management, predictive analysis, etc.

Some of the languages that use logic programming paradigms are:

  • Prolog
  • GHC
  • Polka
  • Vulcan
  • Mercury

Example:

Let us take the statement, “Jimmy is a dog. All dogs are omnivores. So Jimmy is an omnivore”.

So in a Prolog code,

dog(Jimmy)
omnivore(X) :- dog(X)

The first line states Jimmy is a dog. :- can be read as if. So by reading it from right to left, “if something is a dog, then it is an omnivore”.

To test the program, we can ask if Jimmy is an omnivore.

?- omnivore(Jimmy)

Functional Programming

Functional programming is built upon mathematical functions. It solves problems using these mathematical functions as program components. This programming is used while working with concurrency and parallelism.

The paradigm algorithm consists of two components: logic and control. The logic includes facts and rules, and the control contains the order of those rules. The logic specifies what needs to be solved, whereas the control specifies how it should be done.

The advantage of using functional programming is that these programs can run efficiently in multi-threaded and multi-core environments. The programmer only needs to specify the logic and the system automatically finds the best solution.

You can implement things in less number of lines using functional programming. It is also easy to unit test and debug using functional programming.

The disadvantage of functional programming is that there is no for loop and while loops. Instead, we have recursions to iterate through our code.

Example: Javascript code that prints “Hello World” 10 times using functional programming.

Array(10).fill("Hello World")
.map((msg) => {
  console.log(msg);
  return msg;
}); 

Output:

Some of the languages that use Functional programming paradigms are:

  • Haskell
  • JavaScript
  • Scala
  • Erlang
  • Lisp

Final Thoughts

The programming paradigms help us in reducing the complexity of the code. One must know when to use which paradigm according to their problem statement.

If you are just starting out, you can begin with procedural programming. Explore the other types of paradigms after that. Learn and apply them to your problem statements and check how the complexities are reduced.

I hope you found the article useful. If you have any doubts, let me know in the comment section. Happy coding!

Ashwin Joy

I'm the face behind Pythonista Planet. I learned my first programming language back in 2015. Ever since then, I've been learning programming and immersing myself in technology. On this site, I share everything that I've learned about computer programming.

3 thoughts on “Types of Programming Paradigms

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts