Programming Paradigms

Pradeesh Kumar
5 min readJan 18, 2023

--

Programming Paradigm

1. Introduction

Programming paradigms are a way to classify programming languages based on their features. A language can be classified into multiple paradigms. The Programming Paradigm is mainly classified into two types: Imperative and Declarative.

2. Imperative Language

In the Imperative type of Language, The programmers instruct the machine How to perform a task by writing step-by-step instructions including variables, conditions, loops etc.

Example: When you are hungry, you go to the kitchen and make your own food by adding the necessary ingredients and following the cooking instructions.

Programming Example: Languages like C, C++, and Java belong to Imperative Paradigm. Eg: You want to search for an element in an array of numbers, you write all the instructions to perform the search such as taking input, iterating the array, checking if the element is found or not, returning the results, etc.

// Written in Java
// Function to search for a specific element in the array
boolean search(int array[], int key) {
for (int i = 0; i < array.length; i++) {
if (array[i] == key) {
return true;
}
}
return false;
}

3. Declarative Language

In the Declarative type of Language, The programmers instruct the machine What to perform without any instructions. The programmer asks for the desired result.

Example: When you are hungry, you order food from a restaurant. You don’t know how the food is made. You just asked for a desired food and it’s delivered to you.

Programming Example: SQL (Structured Query Language) belongs to the declarative language.

For eg: SELECT * FROM EMPLOYEE in this query, you just mentioned that you need all the employee records from the table EMPLOYEE. However, you didn’t specify step-by-step instructions and logic to retrieve the data from the table. You don’t know the underlying logic used to fetch the records from the database.

4. Imperative Classifications

The imperative paradigm is further classified into two types: Procedural and Object Oriented.

4.1 Procedural Oriented

Procedural language breaks down a task into a collection of procedures(aka. subroutines, functions) to perform a task. It maintains global variables to manage the state of the system. C, BASIC, and ALGOL etc belong to the procedural-oriented paradigm.

Example Program: Linear search in C.

int main()
{
int array[100], key, c, n, found_index;

printf("Enter number of elements in array: ");
scanf("%d", &n);

printf("Enter %d integer(s): ", n);

for (c = 0; c < n; c++) {
scanf("%d", &array[c]);
}

printf("Enter a number to search:");
scanf("%d", &key);

found_index = linear_search(array, n, key);
if (found_index >= 0)
{
printf("%d is present at location %d.\n", key, found_index);
}
else
{
printf("%d not present", key);
}
return 0;
}

int linear_search(int a[], int n, int key) {
long c;

for (c = 0 ; c < n ; c++) {
if (a[c] == key)
return c;
}
return -1;
}

4.2 Object Oriented

Object Oriented language breaks down a task into objects that expose behaviour(method) and data(member or attribute), objects communicate with each other through message-passing techniques to accomplish a given task. C++, Java, C#, Kotlin etc belong to the object-oriented paradigm.

Example: Linear search in Java.

class ArraySearch {

int[] array;

public ArraySearch(int[] array) {
this.array = array;
}

public int linearSearch(int key) {
for (int i = 0; i < this.array.length; i++) {
if (this.array[i] == key) {
return i;
}
}
return -1;
}
}

class MainApplication {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of elements in array: ");
int n = sc.nextInt();
int[] array = new int[n];

System.out.printf("Enter %d integer(s): ", n);
for (int i = 0; i < n; i++) {
array[i] = sc.nextInt();
}

System.out.printf("Enter a number to search:");
int key = sc.nextInt();
ArraySearch arraySearch = new ArraySearch(array);
int result = arraySearch.linearSearch(key);

if (key >= 0) {
System.out.printf("%d present at %d location", key, result);
} else {
System.out.printf("%d not present", key);
}
}
}

5. Declarative Classifications

The imperative paradigm is further mainly classified into two types: Functional and Logical.

5.1 Functional

Functional programming contains only functions without mutating the state or data. It means that there are no global data/variables. Composing and applying function is the main driving force in this paradigm. A function can take another as an argument and return a new function. The first high-level functional programming language, developed in the 1950s, was LISP. This language laid the foundation for many characteristics of modern functional programming languages.

Example: Factorial program using LISP

(defun factorial (num)
(cond ((zerop num) 1)
(t ( * num (factorial (- num 1))))
)
)
(setq n 6)
(format t "~% Factorial ~d is: ~d" n (factorial n))

5.2 Logical

The Logical paradigm has its foundations in mathematical logic in which program statements express facts and rules. Rules are written as logical clauses. The engine infers the answer to the user’s query by using unification and backtracking techniques. The set of facts and rules forms a knowledge base, allowing the system to reason. Prolog, Datalog etc are examples of logical programming. Logical programming helps in major areas such as Natural language processing, Database management, Pattern matching, theorem proving, and Predictive analysis.

Example: Prolog program to infer relationship match of two people from their food interest knowledge base.

/* Facts */
boy(tom). /* read as 'tom is a boy' */
boy(bob).
boy(jerry).

girl(alice). /* read as 'alice is a girl' */
girl(lili).

nonveg(chicken). /* read as 'chicken is a nonveg' */
nonveg(fish).

veg(broccoli). /* read as 'broccoli is a veg' */
veg(apple).

eat(tom, chicken). /* tom eats chicken */
eat(tom, apple). /* tom eats apple */
eat(bob, broccoli).
eat(bob, apple).
eat(alice, chicken).
eat(lili, apple).
eat(lili, broccoli).
eat(jerry, apple).

human(X) :- boy(X) ; girl(X). /* X is a human being if X is either boy or girl */

is_vegetarian(X) :- human(X), eat(X, Y), veg(Y).
is_nonvegetarian(X) :- human(X), eat(X, Y), nonveg(Y)

/* Rules */
nonveg(X) :- food(X). /* if X is a nonveg, then X is a food */
veg(X) :- food(X). /* if X is a veg, then X is a food */

/* X likes if both are vegetarian or both are non vegetarian*/
match(X, Y) :- (is_vegetarian(X), is_vegetarian(Y)) ; (is_nonvegetarian(X), is_nonvegetarian(Y))
/* Query check if bob likes alice */
?-match(bob, alice)

/* Query check if bob likes lili*/
?-match(bob, lili)

--

--

Pradeesh Kumar

Computer Science | Distributed Computing | Databases | Cloud