Procedural vs Object-Oriented Programming Explained
Q: What is the difference between procedural programming and object-oriented programming?
- Programmer
- Mid level question
Explore all the latest Programmer interview questions and answers
ExploreMost Recent & up-to date
100% Actual interview focused
Create Programmer interview for FREE!
Procedural programming and object-oriented programming (OOP) are two fundamental programming paradigms that differ primarily in how they structure and organize code.
Procedural programming is based on the concept of procedures or routines, which are a series of computational steps to be followed. In this paradigm, the focus is on writing procedures or functions that operate on data. The program is structured as a sequence of instructions, with a strong emphasis on the flow of control. A key characteristic is that data and functions are separate; functions manipulate data that is defined globally or passed as arguments. An example of a procedural programming language is C. A simple C program that calculates the factorial of a number illustrates this paradigm:
```c
#include
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
int main() {
int num = 5;
printf("Factorial of %d is %d\n", num, factorial(num));
return 0;
}
```
On the other hand, object-oriented programming organizes code around objects. An object can be thought of as a self-contained entity that combines data and behavior. This paradigm emphasizes encapsulation, inheritance, and polymorphism. Encapsulation allows the internal state of an object to be protected from outside interference and misuse. Inheritance enables a new class to inherit properties and behaviors (methods) from an existing class. Polymorphism allows objects of different classes to be treated as objects of a common super class. An example of an object-oriented programming language is Python. Here’s a simple example of a class that represents a `Car`:
```python
class Car:
def __init__(self, make, model):
self.make = make
self.model = model
def display_info(self):
return f"Car make: {self.make}, Model: {self.model}"
my_car = Car("Toyota", "Corolla")
print(my_car.display_info())
```
In summary, the key differences lie in their approach to structuring code: procedural programming focuses on procedures and function calls, while object-oriented programming focuses on objects that encapsulate data and behavior. This leads to differences in code organization, reusability, and ease of maintenance.
Procedural programming is based on the concept of procedures or routines, which are a series of computational steps to be followed. In this paradigm, the focus is on writing procedures or functions that operate on data. The program is structured as a sequence of instructions, with a strong emphasis on the flow of control. A key characteristic is that data and functions are separate; functions manipulate data that is defined globally or passed as arguments. An example of a procedural programming language is C. A simple C program that calculates the factorial of a number illustrates this paradigm:
```c
#include
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
int main() {
int num = 5;
printf("Factorial of %d is %d\n", num, factorial(num));
return 0;
}
```
On the other hand, object-oriented programming organizes code around objects. An object can be thought of as a self-contained entity that combines data and behavior. This paradigm emphasizes encapsulation, inheritance, and polymorphism. Encapsulation allows the internal state of an object to be protected from outside interference and misuse. Inheritance enables a new class to inherit properties and behaviors (methods) from an existing class. Polymorphism allows objects of different classes to be treated as objects of a common super class. An example of an object-oriented programming language is Python. Here’s a simple example of a class that represents a `Car`:
```python
class Car:
def __init__(self, make, model):
self.make = make
self.model = model
def display_info(self):
return f"Car make: {self.make}, Model: {self.model}"
my_car = Car("Toyota", "Corolla")
print(my_car.display_info())
```
In summary, the key differences lie in their approach to structuring code: procedural programming focuses on procedures and function calls, while object-oriented programming focuses on objects that encapsulate data and behavior. This leads to differences in code organization, reusability, and ease of maintenance.


