Procedural and Object-Oriented Programming

By Shahabuddin Amerudin

Procedural Programming is a programming paradigm that is based on the concept of procedures, which are essentially sets of instructions that tell a computer what to do. The focus of procedural programming is on the step-by-step execution of a series of procedures to accomplish a specific task. In this programming paradigm, the program is organized around the data, and functions are used to manipulate the data.

Object-Oriented Programming (OOP), on the other hand, is a programming paradigm that is based on the concept of objects. In OOP, data and the procedures that operate on that data are combined into a single entity known as an object. The focus of OOP is on the objects and their interactions, rather than on the procedures.

Here are some examples in C++ and VB of procedural and object-oriented programming:

Example of Procedural Programming in C++:

#include <iostream>
using namespace std;

int main()
{
   int a = 5, b = 10;
   int sum = a + b;
   cout << "The sum of " << a << " and " << b << " is " << sum << endl;
   return 0;
}

Example of Procedural Programming in VB:

Private Sub btnSum_Click()
   Dim a As Integer
   Dim b As Integer
   Dim sum As Integer
   a = Val(txtA.Text)
   b = Val(txtB.Text)
   sum = a + b
   lblResult.Caption = "The sum of " & a & " and " & b & " is " & sum
End Sub

Example of Object-Oriented Programming in C++:

#include <iostream>
using namespace std;

class Rectangle {
   private:
      int length;
      int width;

   public:
      Rectangle(int len, int wid) {
         length = len;
         width = wid;
      }

      int area() {
         return length * width;
      }
};

int main() {
   Rectangle rect(5, 10);
   cout << "The area of the rectangle is " << rect.area() << endl;
   return 0;
}

Example of Object-Oriented Programming in VB:

Public Class Rectangle
   Private length As Integer
   Private width As Integer

   Public Sub New(len As Integer, wid As Integer)
      length = len
      width = wid
   End Sub

   Public Function Area() As Integer
      Return length * width
   End Function
End Class

Private Sub btnArea_Click()
   Dim rect As Rectangle
   rect = New Rectangle(5, 10)
   lblResult.Caption = "The area of the rectangle is " & rect.Area()
End Sub

In the procedural programming examples, the focus is on the steps taken to accomplish a specific task, such as calculating the sum of two numbers. In the object-oriented programming examples, the focus is on the object and its properties and behaviors, such as a rectangle and its area.

Procedural programming can be useful in situations where the program’s functionality is relatively simple and doesn’t require a lot of complexity. However, as programs become more complex, the use of procedural programming can lead to code that is difficult to maintain and update.

Object-oriented programming, on the other hand, provides a more structured and organized approach to programming. By encapsulating data and functions into objects, the code becomes more modular and easier to maintain. Additionally, object-oriented programming provides inheritance, which allows new classes to be created based on existing classes, making it easier to reuse code.

In conclusion, both procedural and object-oriented programming have their own strengths and weaknesses, and the choice of programming paradigm depends on the specific requirements of the project. However, as programs become more complex, the benefits of object-oriented programming become more apparent, and it is often the preferred approach to programming.

Suggestion for Citation:
Amerudin, S. (2023). Procedural and Object-Oriented Programming. [Online] Available at: https://people.utm.my/shahabuddin/?p=6165 (Accessed: 28 March 2023).
Scroll to Top