A programming paradigm is a style of programming. A language can have many different paradigms.
There are four major programming paradigms:
- Procedural
- Object Oriented
- Functional
- Logical
These paradigms are categorised into two types:

Imperative
Imperative programming is where statements are structured into procedures that are followed step by step by the computer. Imperative programming languages are therefore known as top-down languages. Most of the early programming languages are all imperative. Here is an example, adding the numbers 0 – 10 in C:
int main()
{
int sum = 0;
sum += 1;
sum += 2;
sum += 3;
sum += 4;
sum += 5;
sum += 6;
sum += 7;
sum += 8;
sum += 9;
sum += 10;
printf("The sum is: %d\n", sum); //prints-> The sum is 55
return 0;
}
In the above example, we are commanding the computer what to do line by line. Finally, we are storing the value and printing it.
Procedural
Procedural is where the functions can be split into procedures. The difference between functions and procedures are functions return a result, where procedures do not. Here is an example in C, doing the same as above, but using a procedure:
int main()
{
int sum = 0;
int i =0;
for(i=1;i<11;i++){
sum += i;
}
printf("The sum is: %d\n", sum); //prints-> The sum is 55
return 0;
}
The functions have been replaced by a for loop, which is a procedure.
Languages that support the procedural programming paradigm are:
- C
- C++
- Java
- ColdFusion
- Pascal
Object Oriented
Object Oriented Programming (or OOP) is the most popular, as it allows you to modularise your code. The key characteristics of object-oriented programming include Class, Abstraction, Encapsulation, Inheritance and Polymorphism.

Object
Any entity that has state and behavior is known as an object. For example, a chair, pen, table, keyboard, bike, etc. It can be physical or logical.
https://www.javatpoint.com/java-oops-concepts
Objects can be defined as an instance of a class, and objects will generally have properties.
Class
A class can also be defined as a blueprint from which you can create an individual object. Class doesn’t consume any space.
https://www.javatpoint.com/java-oops-concepts
A class lets you create objects based on certain criteria. For example we could have a Dog class:
public class Dog (
Breed breed,
Colour colour,
String name
)
Then you could create a new Dog object, by passing in the variables required:
Dog bob = new Dog(Labrador, Chocolate, Bob)
Inheritance
When one object acquires all the properties and behaviors of a parent object, it is known as inheritance.
https://www.javatpoint.com/java-oops-concepts
Inheritance allows you to re-use code you have already written. For example if you had a class called Bike:
public class Bike(){
public int wheels() {
return 2;
}
}
Bike has a method called wheels which returns 2. If you now wanted to create a class for Motorbike, you could extend Bike, to inherit the wheels method, and add more on.
public class Motorbike extends Bike {
public boolean engine() {
return true;
}
}
If we created a new Motorbike object, it would now have the engine method, but also would still have the wheels method, as it inherits from the Bike parent class.
Polymorphism
Inheritance also allows for Polymorphism.
If one task is performed in different ways, it is known as polymorphism.
https://www.javatpoint.com/java-oops-concepts
So for example we could have a Shape class:
public class Shape() {
public int sides() {
return 0;
}
}
The Shape class has a sides method which returns 0, which means it isn’t really a shape. We could then create a class that inherits from Shape:
public class Square extends Shape {
@Override
public int sides() {
return 4;
}
}
We use the @Override annotation to change what the sides method returns for our Square class. Now any Square objects would return 4, but Shape objects still return 0. This is Polymorphism.
Abstraction
Abstraction is a process of hiding the implementation details from the user. Оnly the functionality will be provided to the user.
https://javatutorial.net/java-abstraction-example
In Abstraction, there will be an abstract parent class, which has methods defined, but has no actual implementation. Then classes can extend the abstract class and implement the methods themselves.
public abstract class Employee() {
public int salary();
}
public class FullTimeEmployee extends Employee {
public int salary() {
return 100000;
}
}
public class PartTimeEmployee extends Employee {
public int salary() {
return 50000;
}
}
Encapsulation
Encapsulation in Java is a mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit. In encapsulation, the variables of a class will be hidden from other classes, and can be accessed only through the methods of their current class. Therefore, it is also known as data hiding.
https://www.tutorialspoint.com/java/java_encapsulation.htm
So basically Encapsualtion means we don’t want variables in a class to be accessed directly. Therefore methods are created that are public that allow access, while the variable itself is still private. Here is an example:
public class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String newName) {
name = newName;
}
public int getAge() {
return age;
}
public void setName(int newAge) {
age = newAge;
}
}
The getter and setter methods are used to interact with the variables in the class.
Languages that support the object-oriented paradigm:
- Python
- Ruby
- Java
- C++
Declarative
Declarative programming is a programming paradigm in which the programmer defines what needs to be accomplished by the program without defining how it needs to be implemented. In other words, the approach focuses on what needs to be achieved instead of instructing how to achieve it.
Functional
The key principle of this paradigm is the execution of a series of mathematical functions. You compose your programme of short functions, all code is within a function and all variables are only scoped to the function.
Functional programming consists only of PURE functions. Pure functions are those which take an argument list as an input and whose output is a return value. It may feel like all functions are pure, however if they rely on a global variable or anything that is not directly passed in, then they are not pure.
Here is an example in JavaScript:
function isPrime(number){
for(let i=2; i<=Math.floor(Math.sqrt(number)); i++){
if(number % i == 0 ){
return false;
}
}
return true;
}
isPrime(15); //returns false
For and while loops are not supported in functional progamming. Recursion is used instead, where a function is called multiple times to achieve a loop. Here is an example of recursion:
Function Count (integer N)
if (N <= 0) return "Must be a Positive Integer";
if (N > 9) return "Counting Completed";
else return Count (N+1);
end function
The method above counts from a number up to 10, printing each number. Count(7) would print 8,9,10. Recursion can cause issues such as infinite loops, but so can for and while loops.
Functional methods should have referential transparency, which means the expression should be able to be replaced by the result without changing the programmes behaviour. For example:
int add(int a, int b)
{
return a + b
}
This is referentially transparent as the two below are the same:
int x = add(1, 2)
int x = 3
Functions should be First Class, which means they could be passed into another function as an argument with no issues. Being referentially transparent allows this to happen. Functions are High Order if they accept a First Class function as an argument.
Variables are Immutable in functional programming as you cannot modify a variable after it has been initialised. You can create new variables and this helps to maintain state throughout the runtime of a programme.
Languages that support functional programming paradigm:
- Haskell
- OCaml
- Scala
- Clojure
- Racket
- JavaScript
Logical
The logic programming paradigm isn’t made up of instructions – rather it’s made up of facts and clauses. It uses everything it knows and tries to come up with the world where all of those facts and clauses are true.
For instance, Socrates is a man, all men are mortal, and therefore Socrates is mortal. Here is an example in Prolog:
man(Socrates).
mortal(X) :- man(X).
The first line can be read, “Socrates is a man.” It is a base clause, which represents a simple fact.
The second line can be read, “X is mortal if X is a man;” in other words, “All men are mortal.” This is a clause, or rule, for determining when its input X is “mortal.” (The symbol :-, sometimes called a turnstile, is pronounced if.) We can test the program by asking the question:
?- mortal(Socrates).
that is, “Is Socrates mortal?” (The ?- is the computer’s prompt for a question). Prolog will respond yes.
Languages that support the logic programming paradigm:
- Prolog
- Absys
- ALF (algebraic logic functional programming language)
- Alice
- Ciao
Useful Resources
A lot of this was new to me, so the below are the resources I used mainly to learn about them.
- https://www.freecodecamp.org/news/what-exactly-is-a-programming-paradigm/
- https://hackr.io/blog/programming-paradigms
You can find the list of all of my Knowledge Sharing posts here.

One thought on “Programming Paradigms”