What are programming paradigms? - Part 5
This article compares procedural, object-oriented, functional, declarative and event-driven programming paradigms using the same grocery billing example and different ways to think about solutions for beginners.
What are programming paradigms? - Part 5
If you give the same problem to five experienced programmers, you may get five different solutions, and all of them can be correct. One will write a list of steps, one will create objects which talk to each other, one will chain small functions, one will just describe the result he wants and one will write code which waits for the user to do something. These are not only differences in the language, these are different ways of thinking about the solution, which are called as programming paradigms. In the previous article, we discussed the common data structures and how to choose the right one. In this article, we are going to see what is a programming paradigm and the five common paradigms, procedural, object-oriented, functional, declarative and event-driven, with the same shop billing example in each.
Thinking Like a Programmer series:
- What is computational thinking? - Part 1
- How to solve problems with algorithms? - Part 2
- How data and information are represented in a computer? - Part 3
- What are data structures? - Part 4
- What are programming paradigms? (this article)
TL; DR Summary
- A programming paradigm is a style or way of thinking to structure the solution of a problem.
- Procedural - The program is a sequence of steps and procedures which change the data.
- Object-oriented - The program is a set of objects, each object keeps its data and behaviour together.
- Functional - The program is built by combining functions which don’t change any data.
- Declarative - We describe what result we want, not how to get it.
- Event-driven - The program waits and reacts to events like a button click.
- Most modern languages support more than one paradigm, choose the paradigm which fits the problem.
What is a programming paradigm?
A programming paradigm is a fundamental style of programming, a set of ideas and principles which defines how we structure and organize the solution of a problem.
In simple terms, a paradigm is a way of thinking. Let us take an analogy, there are different ways to reach from Chennai to Bangalore, by train, by bus, by car or by flight. All of them reach the same place, but the way we plan, the cost and the comfort are different. Similarly, all paradigms can solve the problem, but the way we think, the structure of the code and the strengths are different.
Be mindful that a paradigm is not the same as a language. A language can support one or more paradigms. C is mainly procedural, but Java and C# support object-oriented, and since few years, also functional style. The following diagram depicts the five common paradigms with example languages for each.

To compare them easily, we will calculate the same bill total in each paradigm.
Procedural programming
Procedural programming is a paradigm where the program is written as a sequence of instructions, grouped into procedures (also called as functions or subroutines), which work on shared data step by step.
It is the most natural way for beginners, since it is exactly how we write an algorithm, step 1, step 2, step 3. C, Pascal and FORTRAN are the procedural languages.
Example:
float calculate_total(float prices[], int qty[], int count)
{
float total = 0;
int i;
for (i = 0; i < count; i++)
total = total + (prices[i] * qty[i]);
return total;
}
- Pros - Simple, easy to follow, very close to how the computer executes and it is efficient.
- Cons - When the program is growing, the data and the procedures which change it are spread everywhere. A change in the data can break many procedures, so it is hard to maintain large programs.
Object-oriented programming
Object-oriented programming (OOP) is a paradigm where the program is organized as a set of objects, each object combines the data (state) and the operations (behaviour) on that data, and objects work together by sending messages to each other.
In simple terms, we model the solution like the real world. In our shop, there are items and bills. An item knows its name, price and quantity, and it knows how to calculate its own amount. A bill knows its items and how to calculate its total. Java, C# and C++ are the common object-oriented languages.
Example:
class Item {
private double price;
private int qty;
Item(double price, int qty) {
this.price = price;
this.qty = qty;
}
double getAmount() {
return price * qty;
}
}
class Bill {
private List<Item> items = new ArrayList<Item>();
void add(Item item) {
items.add(item);
}
double getTotal() {
double total = 0;
for (Item item : items)
total += item.getAmount();
return total;
}
}
Info: We used double here only to keep the example short. As we discussed in Part 3, use BigDecimal for real money.
- Pros - Data and behaviour are together, so a change is mostly inside one class. Easy to model real-world things and to reuse the code.
- Cons - More code and more design upfront for small problems. A bad class design can make it more complex than procedural code.
Object-oriented thinking is a big topic. In the next series, we will see it in detail, starting from how people classify things in the real world, and then abstraction, encapsulation, inheritance and polymorphism.
Functional programming
Functional programming is a paradigm where the program is built by combining pure functions, functions which always give the same output for the same input and don’t change any data outside them.
It is based on mathematics. In maths, f(x) = x * 2 always gives 10 for 5, it doesn’t change anything else in the world. Instead of changing a total variable in a loop, we transform the list of items into a new value. Haskell, F#, Lisp and Erlang are the functional languages, and C# also supports this style with LINQ.
Example:
In Haskell,
total items = sum (map (\(price, qty) -> price * qty) items)
The same in C# with LINQ,
var total = items.Sum(item => item.Price * item.Qty);
There is no loop and no variable which is changing. We say map each item to its amount, then sum all the amounts.
- Pros - No hidden changes, so it is easy to test and to reason about. Very safe when many tasks are running in parallel on multi-core processors, since nobody is changing the shared data.
- Cons - It is a different way of thinking and it takes time for beginners. Some problems which naturally change the state, like a bank account balance, look less natural.
Declarative programming
Declarative programming is a paradigm where we describe what result we want, and the language or the system decides how to produce it.
We have already seen this in the 4GL section of Understanding Computers and Programming - Part 3. SQL is the best example, HTML is also declarative, we say “this is a heading”, we don’t say how to draw the letters on the screen.
Example:
SELECT SUM(price * qty) AS total
FROM bill_items
WHERE bill_id = 101;
It is like ordering in a restaurant, we say “one masala dosa”, we don’t tell the chef how to make it. Functional programming is also considered as a type of declarative programming, while procedural and object-oriented are called as imperative, where we tell the computer how to do it, step by step.
- Pros - Very short and readable, the system can optimize how to do it.
- Cons - Less control on how it is done, and it is mostly limited to a specific area like databases or web pages.
Event-driven programming
Event-driven programming is a paradigm where the flow of the program is decided by events, like a mouse click, a key press, a message from network or a timer, and the program responds to each event with a piece of code called as event handler.
In the other paradigms, the program decides what to do next. In event-driven, the program waits, and the user or another system decides. Our billing screen doesn’t calculate the total by itself, it waits until the shopkeeper clicks “Calculate”. JavaScript in web pages, Windows Forms and WPF in .NET and almost all mobile and desktop applications are event-driven.
The following diagram depicts the flow of an event-driven program. The flow highlighted in purple is what happens when the user clicks a button which has a handler. After handling, the program goes back to the event loop and waits for the next event.

Example:
document.getElementById("calculate").onclick = function () {
var price = Number(document.getElementById("price").value);
var qty = Number(document.getElementById("qty").value);
document.getElementById("total").innerHTML = price * qty;
};
- Pros - Natural for user interfaces and systems which react to the outside world. The program is idle and not using the CPU when nothing is happening.
- Cons - The flow is not in one place, so it is harder to follow and debug when there are many events. A slow handler blocks other events and the screen freezes.
How to choose a paradigm?
Find below the quick comparison of the five paradigms,
| Paradigm | Thinks in | Example languages | Good for |
|---|---|---|---|
| Procedural | Steps and procedures | C, Pascal | Small programs, system programming |
| Object-oriented | Objects with data and behaviour | Java, C#, C++ | Large business applications |
| Functional | Functions and transformations | Haskell, F#, LINQ in C# | Data processing, parallel work |
| Declarative | The result we want | SQL, HTML | Database queries, page structure |
| Event-driven | Events and handlers | JavaScript, WinForms | User interfaces, servers reacting to messages |
There is no best paradigm, it is a trade-off and the right choice depend on the problem. In fact, a real application uses many of them together. Our shop billing application can have object-oriented classes for items and bills, a functional LINQ query to calculate the total, an SQL query to read the prices from the database and an event-driven screen for the shopkeeper. As a beginner, start with procedural thinking to learn the steps, then move to object-oriented thinking, which is the most used paradigm in the industry today.
Wrap up
In this article, we have looked at what is a programming paradigm and the five common paradigms, procedural, object-oriented, functional, declarative and event-driven, with the same billing example in each. With this, we have completed the series, starting from computational thinking, algorithms, data representation and data structures until the different ways of thinking about a solution. I really appreciate that you took the time to read this series and I hope it provided you some insights into thinking like a programmer. In the next series, I will walk you through the object-oriented thinking, how to model the real world with objects, how we organize what we know with generalization, specialization and hierarchy, and the principles of abstraction, encapsulation, inheritance and polymorphism, each one first from the real world and then in software.
Related Posts
How data and information are represented in a computer? - Part 3
This article explains how computers represent data as bits and bytes, including numbers, text, images and sound, and why representation choices affect grocery billing programs.
How to solve problems with algorithms? - Part 2
This article presents a practical problem-solving process using algorithms, pseudocode, flowcharts, sample tracing, testing and search comparisons for a grocery billing system before writing code.
What are data structures? - Part 4
This article introduces arrays, linked lists, stacks, queues, hash tables, trees and graphs, explaining how data structures organize grocery items, customers and bills for efficient programs.