Programming paradigms

Introduction to programming paradigms

A programming paradigm is simply a way of programming. There are hundreds of different known programming languages. Each of these languages needs to follow some sort of strategy to write programs. That strategy is a paradigm. Some paradigms are easy to follow, while others need deep knowledge and command over them.

Note: – A programming paradigm is not a programming language, rather it is a way in which you write code.

Imperative –

It is an approach in which the programmer commands the computer what to do step by step by changing the state of the program. The state is usually changed by a series of assignment statements.

Below given are the approaches which come under the imperative programming paradigm.

Procedural Oriented Programming –

While learning to program, this is probably the first approach that you would learn. Procedural programming is a paradigm in which a program is divided into procedures which may be routines or functions containing a number of steps. Procedural programming is also one of the categories of imperative approach.

#include <stdio.h>
int main()
{
	int i, ave, result = 0;
	for(i = 1; i <= 5; i++)
	{
		result += i;
	}
	ave = result / 5;
	printf("%d", ave);
	return 0;
}

Examples of programming languages that support procedural paradigms include C, C++, and Java.

Object-Oriented Programming –

Object-oriented programming often abbreviated as OOP is an approach in which all real-world entities are represented through classes. A class is a blueprint or a template from which different objects can be created. It is one of the most popular programming approaches because of its characteristics. One reason behind its popularity is that one can directly associate real-world problems in terms of computer code.

For eg.  If there is a class named ‘Vehicle’, then its objects could be cars, bikes, buses, trucks,s, and all the other vehicles. All of these objects have the same attributes like price, number of tires, number of headlights. And all of them could have the same properties like motion, horn.

The main characteristics of object-oriented programming are Inheritance, Abstraction, Encapsulation, and Polymorphism.

Inheritance – You can have multiple classes within a program. Those classes can be individual or you can inherit a class from another class. Inheritance is the process by which the inherited class (called child class) can acquire the properties of the base class (The class from which the other class is inherited).

Abstraction – Abstraction is the process of hiding the internal details of an application from the user and showing only specific details. This is achieved by the access modifiers by declaring the data members and member functions as public, private, and protected.

Encapsulation – Encapsulation or data encapsulation is the concept of OOP that binds the data and the functions that manipulate that data together.

Polymorphism – It is the combination of two words – ‘Poly’ means many and ‘morphism’ means forms. Polymorphism refers to the ability of the objects of different types to access the functions of the same name.

Some examples of polymorphism are virtual functions, operator overloading, and function overloading.

Declarative –

Declarative programming is an approach in which the programmer focuses on what needs to be achieved instead of focusing on how it can be achieved. Below given are the programming approaches which come under the declarative programming paradigm.

Functional Programming –

It is a programming paradigm in which programs are created by applying functions. Functions are kept at first priority in functional programming. This means in functional programming, a function can be bound to identifiers and can behave like an identifier. The functional programming approach is widely used in statistics and financial analysis.

A function can be passed as an argument or even can be returned from another function just like any other data type.

There is no concept of loops in functional programming. Continuity or iterations in functional programming are achieved by recursion. The reason why this is so is looping requires mutable data. But in functional programming, data is immutable. Mutability is where the state of a variable can be changed within the life of a program.

For Example –

int x = 1 //the value of x is 1.
x = 4 // the value of x is 4 now.
x = 7 // the value of x is 7 now.

In functional programming we cannot have mutable data, therefore there is no concept of looping in functional programming.

If you need to print numbers starting from 1 to 10 through a loop, you would have written something like this-

int x = 1
while(x <= 10)
{
	printf("%d", x);
	x++;
}

In the above loop, the initial value of x is 1, but the value changes with every iteration of the loop

The continuity in functional programming is achieved through recursion. Below given is an example in C++ which will print numbers starting from 1 to 10 without using a loop (rather by using recursion).

#include <iostream>
using namespace std;
void displayNum(unsigned int n) 
{

    if(n > 0)
	{
		displayNum(n - 1);
		cout<<n<<endl;
	}
}
int main()
{
	displayNum(10);
	return 0;
}

Functional programming is not limited to any specific languages, though there are some languages that support it fully. Some most popular languages that support functional programming are Haskell, Closure, Scala, Erlang, F#, and Mathematica.

Logic Programming –

Logic programming is an approach that is based on logic. A logic program is a set of sentences in a logical form, in the form of facts and rules about a particular problem.

  • A fact is a declaration about a problem domain. For eg. “Jumbo is an elephant”.
  • A rule is a conclusion about the facts in the problem domain. For eg. “All elephants are mammals”.
  • A query is a question about the domain. For eg. “Is Jumbo a mammal?”

The most common example of logic programming is Prolog. Prolog is a language following the logic programming paradigm and has a notable role in the fields of expert systems, natural language processing, artificial language.

SQL (Structured Query language) also follows the logic paradigm but it is not a programming language, rather it is a query language. SQL is used to communicate with the databases. A database is a collection of data stored in the form of database tables. A wide variety of web and mobile applications need a database to store user information. SQL is a standard language used for storing, retrieving, updating, and deleting data in the database.