Difference between procedural and object-oriented programming with example

AKCoding.com
2 min readMar 22, 2024

--

Difference between procedural and object-oriented programming with example
Difference between procedural and object-oriented programming with example

Procedural programming and object-oriented programming (OOP) are two different paradigms used in software development. Here’s a comparison between the two with examples:

Procedural Programming:

  1. Focus: Procedural programming focuses on creating a step-by-step procedure or algorithm to solve a problem. It revolves around functions or procedures that manipulate data.
  2. Data and Functions: In procedural programming, data and functions are treated separately. Data is stored in variables, and functions operate on this data.
  3. Top-down Approach: Procedural programming typically follows a top-down approach, where the main program calls various functions to accomplish a task.
  4. Example: Consider a program to calculate the area of a rectangle in procedural programming:
# Procedural Programming Example (Python)
def calculate_area(length, width):
return length * width
# Main Program
length = 5
width = 3
area = calculate_area(length, width)
print("Area of the rectangle:", area)

Object-Oriented Programming (OOP):

  1. Focus: Object-oriented programming focuses on modeling real-world entities as objects that have data (attributes) and behavior (methods).
  2. Objects: In OOP, objects are instances of classes. Classes define the structure and behavior of objects.
  3. Encapsulation: OOP promotes encapsulation, where data and methods that operate on the data are bundled together within a class, providing data hiding and abstraction.
  4. Example: Consider a program to model a rectangle using OOP principles:
# Object-Oriented Programming Example (Python)
class Rectangle:
def __init__(self, length, width):
self.length = length
self.width = width
    def calculate_area(self):
return self.length * self.width
# Main Program
rectangle = Rectangle(5, 3)
area = rectangle.calculate_area()
print("Area of the rectangle:", area)

Comparison:

  • Procedural programming focuses on procedures or functions that manipulate data, whereas OOP focuses on modeling real-world entities as objects with data and behavior.
  • In procedural programming, data and functions are treated separately, while OOP promotes encapsulation by bundling data and methods together within classes.
  • Procedural programming follows a top-down approach, whereas OOP promotes a bottom-up approach, where objects collaborate to achieve a task.

In summary, while both paradigms have their strengths and weaknesses, object-oriented programming is often preferred for its modularity, reusability, and ease of maintenance, especially for larger and more complex software projects.

--

--

AKCoding.com
AKCoding.com

Written by AKCoding.com

Empowering developers with programming concepts and code (Mobile & Web Developments using JAVA, React, React Native, JavaScript, Kotlin, Python, .Net, and More)

No responses yet