Most of us learn programming in C first. We write main(), a few functions, some arrays, and the program works. So a fair question is: why do we need a different style at all?
In this chapter we will write the same small program in two styles, procedural and object-oriented, and see exactly where they differ. This comparison is a favourite university exam question ("differentiate between POP and OOP") and a common interview warm-up.
What Is Procedural Programming?
Procedural programming (POP) is a paradigm in which a program is written as a sequence of procedures (functions) that operate on data. Data and functions are kept separate, and data is passed to or shared between functions.
The program is designed top-down: you start with the main task, break it into smaller steps, and write one function for each step.
Generate result sheet
|
+----------------+----------------+
| | |
read marks calculate average print report
|
+------+------+
| |
print one row decide pass/failClassic procedural languages are C, Pascal, FORTRAN and COBOL. The focus is on what steps to perform.
What Is Object-Oriented Programming?
As we saw in Introduction to OOP, OOP organises the program around objects that bundle data with the functions that work on it. The design usually starts bottom-up: first identify the objects (Student, Course, Result), then make them cooperate. The focus is on which things exist and what each is responsible for.
Example: A Result Sheet in C (Procedural)
We keep three students' names and marks, print a report, compute the average and give a grace mark.
#include <stdio.h>
#define MAX 3
/* Shared (global) data: every function can read and change it */
char names[MAX][20] = {"Aditi", "Kabir", "Meera"};
int marks[MAX] = {78, 91, 66};
void print_report(void) {
for (int i = 0; i < MAX; i++)
printf("%-6s %3d %s\n", names[i], marks[i], marks[i] >= 40 ? "PASS" : "FAIL");
}
double average(void) {
int sum = 0;
for (int i = 0; i < MAX; i++)
sum += marks[i];
return (double) sum / MAX;
}
void add_grace(int student, int grace) {
marks[student] += grace; /* nothing stops marks going above 100 */
}
int main(void) {
print_report();
printf("Average: %.2f\n", average());
add_grace(1, 15); /* Kabir now has 106 out of 100! */
marks[2] = -5; /* any code can write garbage directly */
printf("\nAfter changes:\n");
print_report();
return 0;
}Output (compiled with gcc -std=c17):
Aditi 78 PASS
Kabir 91 PASS
Meera 66 PASS
Average: 78.33
After changes:
Aditi 78 PASS
Kabir 106 PASS
Meera -5 FAILThe program is short and clear, and for a program this size it is perfectly fine. But look at what went wrong:
Kabir has 106 out of 100. The rule "marks must stay between 0 and 100" is not written anywhere. Every function that changes marks would have to remember it.
Meera has −5. The
marksarray is global, so any line in the whole program can put any value into it.Data and its rules are scattered. The pass mark (40) is inside
print_report. If another function also needs to know who passed, it will probably copy the>= 40check, and one day the copies will disagree.Parallel arrays are fragile. A student's name is in
names[i]and marks inmarks[i]. Sort one array and forget the other, and every student gets someone else's marks.
In a 50-line program you can keep all of this in your head. In a 50,000-line program written by twenty people, you cannot.
The Same Program in Java (Object-Oriented)
class Student {
private final String name;
private int marks;
Student(String name, int marks) {
this.name = name;
setMarks(marks);
}
private void setMarks(int marks) {
if (marks < 0 || marks > 100) {
throw new IllegalArgumentException("marks must be 0-100, got " + marks);
}
this.marks = marks;
}
void addGrace(int grace) {
setMarks(Math.min(100, marks + grace)); // the rule lives in ONE place
}
int getMarks() { return marks; }
boolean hasPassed() { return marks >= 40; }
String report() {
return String.format("%-6s %3d %s", name, marks, hasPassed() ? "PASS" : "FAIL");
}
}
class Main {
public static void main(String[] args) {
Student[] students = {
new Student("Aditi", 78),
new Student("Kabir", 91),
new Student("Meera", 66)
};
for (Student s : students) System.out.println(s.report());
students[1].addGrace(15); // capped at 100
System.out.println("\nAfter grace:");
System.out.println(students[1].report());
try {
new Student("Test", -5);
} catch (IllegalArgumentException e) {
System.out.println("Rejected: " + e.getMessage());
}
}
}Output:
Aditi 78 PASS
Kabir 91 PASS
Meera 66 PASS
After grace:
Kabir 100 PASS
Rejected: marks must be 0-100, got -5What changed?
A student's name and marks travel together in one
Studentobject. There are no parallel arrays to keep in sync.marksisprivate. Code outside the class cannot writestudents[2].marks = -5; the compiler rejects it.The 0–100 rule and the pass mark live in one place, inside the class. Grace marks are automatically capped.
Invalid data is rejected at the moment the object is created, so a
Studentwith −5 marks can never exist.
Yes, the OOP version is a little longer. That extra code buys safety, and the payoff grows as the program grows.
How the Two Styles Organise a Program
PROCEDURAL OBJECT-ORIENTED
Global / shared data +-------------------+
+------------------+ | Student |
| names[], marks[] | | data: name, marks|
+------------------+ | addGrace() |
^ ^ ^ | hasPassed() |
| | | | report() |
print_ average() add_ +-------------------+
report() grace() ^
| messages
Any function can read/write +-------------------+
any data. | Main / other |
| objects |
+-------------------+
Data is reached only
through the object's methods.Procedural vs Object-Oriented: Comparison Table
Basis | Procedural (POP) | Object-Oriented (OOP) |
|---|---|---|
Basic unit | Function / procedure | Object (made from a class) |
Focus | The sequence of steps (algorithm) | The data and the things (objects) in the problem |
Design approach | Top-down | Bottom-up (usually) |
Data and functions | Kept separate | Bundled together in a class |
Data access | Data often global or freely passed around | Data accessed through methods; access modifiers control visibility |
Data hiding / security | Little or none at the language level | Supported with |
Reuse | Reuse of functions and libraries | Reuse of whole classes, plus inheritance and composition |
Adding new kinds of data | Often requires editing many functions | Often just a new class (thanks to polymorphism) |
Communication | Functions call each other and pass data | Objects send messages (call methods) to each other |
Real-world modelling | Weak: real things are split into data + functions | Strong: real things map to objects |
Examples | C, Pascal, FORTRAN, COBOL | Java, C++, C#, Python, Kotlin, Smalltalk |
Best suited for | Small programs, scripts, low-level and algorithm-heavy code | Large, long-lived applications with many entities and developers |
Careful with one popular row: many notes say "POP does not support overloading, OOP does". That is really a property of specific languages. C has no function overloading, but C++ supports overloading of free functions too, even without classes. In an exam, write it as "C does not support function overloading; C++ and Java do."
What Happens When Requirements Change?
Software keeps changing, so the real test of a style is how easily code adapts. Suppose the university says: "From next semester, students also have internal marks (out of 40), and the final score is external + internal."
Procedural version
Add a new global array
internal[MAX].Find every function that uses
marks[i]as "the score" (print_report,average, maybe ten others in other files) and change each one.Hope nobody forgets one.
Object-oriented version
Add an
internalfield toStudentand update the methods of that one class (for example, atotalScore()method).Code outside the class that only calls
report()orhasPassed()keeps working unchanged.
This idea, "a change should be made in one place", is at the heart of good OOP design. We will formalise it later in Coupling & Cohesion and the SOLID chapters.
Is Procedural Programming Bad?
No. Both are tools, and each fits different jobs.
Operating systems and embedded code are still mostly written in C. The Linux kernel is procedural C, yet it uses OOP ideas like structures of function pointers (for example, a
struct file_operationsthat each driver fills in) to get polymorphism-like behaviour.Short scripts, quick data processing and competitive-programming solutions are usually simpler as a few functions.
Algorithms (sorting, graph search, DP) are naturally written as procedures, even inside OOP languages.
On the other hand, banking systems, e-commerce platforms, Android apps, games and enterprise software, with many entities and many developers over many years, benefit greatly from OOP.
Most real languages are multi-paradigm
C++ lets you write plain C-style functions or full class hierarchies. Python lets you write a script with no classes, or a large OOP application. Java forces code into classes but now also has lambdas and streams from functional programming. In practice, good programmers mix styles: objects for the overall structure, clean procedural or functional code inside methods.
Common Mistakes Students Make
Saying "C++ is purely object-oriented". C++ is multi-paradigm; you can write a C++ program with no classes.
Saying "procedural code cannot be modular". Procedural code can be very modular (separate files, header interfaces, static functions in C). OOP simply gives stronger, built-in tools for it.
Writing procedural code inside classes and calling it OOP: one giant class with all-static methods and public arrays is still procedural.
Confusing top-down and bottom-up. POP: top-down (break the task into steps). OOP: bottom-up (build objects, then assemble them).
Interview / Exam Questions
1. Differentiate between procedural and object-oriented programming (any five points).
(1) Basic unit: function vs object. (2) Focus: steps/algorithm vs data and entities. (3) Design: top-down vs bottom-up. (4) Data and functions: separate vs bundled. (5) Data hiding: weak vs supported through access modifiers. (6) Reuse: functions vs classes with inheritance/composition. (7) Examples: C, Pascal vs Java, C++, C#.
2. Why is data in procedural programs considered less secure?
Data is often global or passed freely between functions, so any function can modify it, and the rules about valid values are scattered. In OOP, data can be made private and changed only through methods that enforce the rules.
3. Which approach does OOP follow, top-down or bottom-up?
Bottom-up: you first design the individual objects/classes and then combine them to build the system. Procedural programming follows top-down decomposition of the main task into sub-tasks.
4. Can you write object-oriented code in C?
You can imitate it. A struct can hold data plus function pointers, and you can hide data by exposing only an opaque pointer and functions in a header. But C provides no built-in classes, access modifiers, inheritance or automatic dynamic dispatch, so it is much more manual and error-prone.
5. Give one situation where you would prefer procedural code.
A small, one-off script (for example, renaming files or summing a CSV column), low-level embedded firmware with tight memory limits, or a self-contained algorithm such as a sorting routine. Adding classes there adds complexity without much benefit.
Summary
Procedural programming organises a program as functions acting on separate data, designed top-down.
OOP organises a program as objects that bundle data and behaviour, usually designed bottom-up.
Procedural code makes it easy for any function to corrupt shared data and scatters business rules; OOP keeps rules next to the data and hides the data.
OOP shines when programs are large, long-lived and change often; procedural code is great for small programs, low-level code and algorithms.
Modern languages are multi-paradigm, and good code mixes both styles sensibly.
Next, in Classes & Objects, we look closely at the two building blocks of every OOP program.