Showing posts with label Programming paradigm. Show all posts
Showing posts with label Programming paradigm. Show all posts

Monday, May 28, 2018

Programming paradigm

Various programming paradigms

Programming paradigm is a way to classify programming languages according to their style of programming and features they provide. There are several features that determine a programming paradigm such as modularity, objects, interrupts or events, control flow etc. A programming language can be single paradigm or multi-paradigm.

With the wide spread of programming, various programming paradigms came into existence. Some popular programming paradigms are:

Imperative programming

Imperative programming is the oldest paradigm and is still in practice. It is the widely practiced paradigm in the day-to-day programming. It mainly focuses on steps to be done and works on the logic of “First do this then do that”. It defines a sequence of statements in order of which the operations must take place. In imperative programming, the control flow is explicit and depend on collection of GOTO statements. Imperative programming lacks the support of modularity.

Examples of imperative programming languages are – Assembly, C, C++, Java etc.

Below is the imperative programming approach of listing the first and last name of students from a list student whose marks is greater than 90.

names = []
i =
totalStudents = (student)
start:
i >= totalStudents then goto end
current = student[i]
marks = current.marks
marks < goto next
(names, current.firstname + current.lastname)
next:
i = i +
goto start
end:
print names

Declarative programming

Declarative programming focuses on the logic of computation neglecting the control flow. It specifies what the result should be, without specifying how to obtain the result. Declarative programming generally does not contain if-else, loops and other control flow statements. Imperative programming defines each minute steps to get the result, whereas in contrast declarative programming only defines the logic of computation.

Popular declarative programming languages are: SQL, XQuery

Below is the declarative programming approach to get first and last name of students from a list student whose marks is greater than 90.

firstname, lastname
student
student.marks >=

Structured programming

Structured programming is a kind of imperative programming, focuses on modular programming. It makes extensive use of for, while, subroutines, blocks and sub-blocks, rather than simply using GOTO which leads to complex and tangled code. Structured programming was introduced as an improvement over the imperative programming approach to get more control over the program.

Examples of structured programming languages are – C, C++, Pascal, Ada etc.

Below is the structured programming approach to get first and last name of students from a list student whose marks is greater than 90.

names = []
i = , i <= (student), i = i + 1 {
student[i].marks >= {
(names, student[i].firstname, student[i].lastname)
}
}
i = , i <= (names), i = i + 1 {
(names[i])
}

Procedural programming

Procedural programming is derived from structured programming. It inherits all properties of structured and imperative programming. Procedural programming is based on procedural calls. Each statement of a procedural language is either procedure call or data assignment.

Note: Procedures, routines, subroutines and functions all are same thing with little difference.

Some popular procedural programming languages are: C, Pascal, BASIC, Fortran

Below is the procedural programming approach to get first and last name of students from a list student whose marks is greater than 90.

void () {
names = ()
(names)
} names () {
names = []
(i = ; i <= (student); i = i + ) {
(student[i].marks >= ) {
(names, student[i].firstname, student[i].lastname)
}
}
names
}
void (names) {
(i = ; i <= (names); i = i + ) {
(names[i])
} }

Object oriented programming

Object Oriented Programming paradigm is widely practiced programming paradigm. It is based on the concept of objects. Objects are real world entity. Everything present around us is an object. Every object has two important property attribute (data) and behavior (function).

For example:

Car is an object having

attributes – type, color, model etc.

behavior – turnLeft(), turnRight(), accelerate() etc.

Object-Oriented programming is also inherited from imperative and procedural programming.

Popular object oriented programming languages are – Simula-67, Java, C++, C# etc.

Below is the object oriented programming approach to get first and last name of students from a list student whose marks is greater than 90.

s in student {
s.marks >= {
(s.firstname + s.lastname);
}
}

Functional programming

Functional programming paradigm is completely different programming approach from all paradigms mentioned above. Functional programming uses a combination of functions calls to drive the flow of the program. The result of a function becomes the input to another function.

Popular functional programming languages are – Python, Lisp, Clojure, Haskell etc.




Instagram