Procedural Language vs. Object-Oriented Language - Key Differences
Tags: programming, coding, webdev
Published: 2022-11-09 10:48:58

In this article, we will be learning the key differences between procedural and object-oriented programming languages. If you are new to these terminologies, we will be covering them in this article so keep reading.

## What is Procedural Programming?
It is defined as a programming language that is inherited from structural programming and is dependent on the concept called the [procedures](https://www.ibm.com/docs/en/db2-for-zos/11?topic=procedure-languages-used-create-stored-procedures). It is also known as routines or subroutines, functions, or methods that comprises of series of computational steps that are to be done during the execution of the program. Any given procedure can be called out at any time during the execution of the program.

## Example of procedural programming

```
#include <stdio.h>
void function1(){
   printf(“Hello”);
   printf(“World”);
}
int main()
{
   printf(“Hey”);
   function1();
   printf(“Welcome”);
   int n = 63;
   if(n > 0){
      printf(“The code is executed”);
   } else {
printf(“Number is less than 0”)
   }
   return 0;
}
```
The languages that are used in procedural programming are as follows:

Fortran, COBOL, pascal, basic, and [C Language](https://www.scaler.com/topics/c/) etc

## Advantages of procedural programming

- It has a simple, straightforward and intuitive way of writing a code and hence good for beginners.
- It is easy to track the flow of the procedural program and to understand how the objects are interacting with the functions and classes.

## Disadvantage of procedural programming
- The code is not reusable and hence the replication of code increases
- It is not easy to scale for complex and larger applications
Due to the visibility of the data, it is not secure 

## What is Object-Oriented Programming?

It is defined as a programming language that uses the concept of an object in programming. The object contains data which are in the form of attributes and the code is in the form of methods. 

The programs of computers are designed by using this concept of an object in an object-oriented programing language that it interacts with the real world. 

There are various types of object-oriented programming languages but the most used ones are class-based where objects are instances of a class and are determined by the type of the object.

When the code is executed, it uses an appropriate class to make an object which is an instance of that class. That object will have state and access to all of the behavior defined by its class.

State also known as an instance variable:

- Each object, or instance of a class will have its own unique set of instance variables as defined in the class. 
- Collectively, the values assigned to an object instances variables make up the object's state.

## Example of object-oriented programming 

```
class Dog{
    constructor(name, birthday) {
        this.name = name;
        this.birthday = birthday;
    }

    //Declaring private variables
    attendance = 0;

    getAge() {
        //Getter
        return this.calcAge();
    }

    calcAge() {
        //calculate age using today's date and birthday
        return Date.now() - this.birthday;
    }
    
    bark() {
        return console.log("Woof");
    }

    updateAttendance() {
        //add a day to the dog's attendance days at the petsitters
        this.attendance++;
    }
}
```

Encapsulation of objects is done in an object-oriented programming language.

If we want maintainability, flexibility, and extensibility our design must include encapsulation by
- Keep instance variables private
- Keeping the accessor method public

The languages that are used in object-oriented programming are as follows:
[Java](https://www.scaler.com/topics/java/), C++, Python, PHP, C#, JavaScript, Swift, etc.

The principles of object-oriented programing are as follows:

**Polymorphism** - it is defined as an ability to share multiple objects at the same name but having different structures or functionalities depending on the context. It is mostly observed in method overloading and overriding. 

**Inheritance** - it is the mechanism of serving one class from another. The class from which other classes are derived is called a superclass. The class which is derived from the superclass is called a subclass. The subclass is a specialized version of the superclass or it can be said that the subclass is the extended version of the superclass. 

**Encapsulation** - it is defined as a wrapping of data into a unit. In simple terms, it means wrapping of classes or objects which means the attributes and state of objects with their methods or behavior. 

**Abstraction** - it is defined as an ability of a class to show certain data or attributes of an object while keeping other data private. This is done in order to hide the implementation details for the user. 

## Advantages of OOPs
- **Simple** - it has simple syntax, clean and easy to understand
- **Object-oriented** - all the code is managed with classes and objects
- **Robust**- the programs are robust in nature as it provides automatic garbage collection for unused objects and also provide appropriate ways of exception handling which would prevent the system from crashing.
- **Dynamic**- the object-oriented programming languages are dynamic i.e, it is capable of linking new class libraries, methods, and objects. The linking depends on the runtime query and the response of the user i.e whether to execute a particular code or to abort the process.
- **Secure**- since the program is made up of classes and objects, it is secure as compared to another programming language.

## The disadvantage of OOPs
- Planning of entities, objects, and their linking beforehand
- The OOps programs are relatively larger as compared to others
- Difficult to implement

## Procedural Programming vs Object-Oriented Programming

Here is a few differences between Procedural Programming vs Object-Oriented Programming

| Procedural programming language |  Object-oriented programming language |
|-----|---|
|  When the program is divided into smaller sections called functions it is called procedural programming.   | When the program is divided into smaller sections called objects, it is called object-oriented programming.  |
|The top-down approach is followed in procedural programming.|The bottom-up approach is followed in object-oriented programming.| 
|The procedural programming has no access specifier.|Object-oriented programming has access specifiers for instance, public, private, protected, etc.|
|Addition of new function and data is not easy|Addition of new function and data is easy|
|Procedural programming is less secure since it does not have a proper way of hiding the data.|Object-oriented programming is more secure since it provides hiding the data.|
|Overloading methods is not possible in procedural programming.|Overloading of methods is possible in object-oriented programming.|
|The concept of inheritance and data hiding is not there in procedural programming.|The concept of inheritance and data hiding is used in object-oriented programming languages.|
|The function is more important as compared to data in procedural programming.|The data is more important as compared to functions in object-oriented programming.|
|Procedural programming is generally based on an unreal-world example.|Object-oriented programming is mostly based on real-world examples.|
|For designing medium-sized programs, procedural programming is used. |For designing large and complex issues, object-oriented programming is used.|
|The concept of procedure abstraction is used in this.|The concept of data abstraction is used in this.|
|The reusability of code is absent in procedural programming.| The reusability of code is present in object-oriented programming.|
|Examples of this are Basic, FORTRAN, C, pascal, etc.|Examples of this are Java, Python, C++, C#, etc.|

## Conclusion
Programming language that is inherited from structural programming and is dependent on the concept called the procedures. 

It is also known as routines or subroutines, functions, or methods that comprises of series of computational steps that are to be done during the execution of the program. Any given procedure can be called out at any time during the execution of the program.

Object-oriented language is defined as a programming language that uses the concept of an object in programming. The object contains data which are in the form of attributes and the code is in the form of methods. 

The programs of computers are designed by using this concept of an object in an object-oriented programing language that it interacts with the real world. 

There are various types of object-oriented programming languages but the most used ones are class-based where objects are instances of a class and are determined by the type of the object.