Chapter One 1.1 Type of Paradigm
Chapter One 1.1 Type of Paradigm
Paradigm can also be termed as method to solve some problem or do some task. A programming
paradigm is the process of writing code in an organized manner based on some specific
methodology/method using programming language. Each language is used as a tool for solving a problem
and has its own unique style that uses a programming paradigm. It is an approach to solve problem using
some programming language or also we can say it is a method to solve a problem using tools and
techniques that are available to us following some approach. There are lots for programming language
that are known but all of them need to follow some strategy when they are implemented and this
methodology/strategy is paradigms. Apart from varieties of programming language there are lots of
paradigms to fulfill each and every demand. They are discussed below:
1. Imperative programming paradigm: It is one of the oldest programming paradigm. 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.
It performs step by step task by changing state. The main focus of this paradigm is on how to achieve the
goal. The paradigm consist of several statements and after execution of all the result is stored. Let us
understand imperative programming using a C++ program that gives us factorial of a number.
Example:
#include<iostream>
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.
Advantages:
Disadvantage:
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. Factorial of n number is the best example of procedural programming paradigm.
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). The program is written in this
paradigm as a collection of classes and object which are meant for communication. The
smallest and basic entity is object and all kind of computation is performed on the objects
only. Objects can have data associated with them called attributes and actions they can
perform using methods. More emphasis is on data rather procedure. It can handle almost all
kind of real life problems which are today in scenario.
Advantages:
Data security
Inheritance
Code reusability
Flexible and abstraction is also present
// Class Signup
class Signup {
int userid;
string name;
string emailid;
char sex;
long mob;
public:
// Function to create and object using
// the parameters
void create(int userid, string name, string emailid,
char sex, long mob)
{
cout << "Welcome to GeeksforGeeks\nLets create "
"your account\n";
this->userid = 132;
this->name = "Radha";
this->emailid = "[email protected]";
this->sex = 'F';
this->mob = 900558981;
cout << "your account has been created" << endl;
}
};
// Driver Cpde
int main()
{
cout << "GfG!" << endl;
// Creating Objects
Signup s1;
s1.create(22, "riya", "[email protected]", 'F', 89002);
return 0;
}
Java
Python3
C#
Javascript
Output
GfG!
Welcome to GeeksforGeeks
Lets create your account
In logical programming the main emphasize is on knowledge base and the problem. The
execution of the program is very much like proof of mathematical statement, e.g., Prolog
Let us take the statement, “abebe is a human. All humans are animals, So abebe is an animal”.
So in prolog code:-
Human(abebe)
Animal(X) :-human(X)
To test the program, we can ask if Abebe is an animal.
?- animal(abebe)