Programming paradigms

Programming paradigms – what is procedural programming?

DiProgramming paradigms are different styles, different “ways” of programming. There are a lot of different paradigms, including some popular ones like procedural programming, object-oriented programming, functional programming, etc. And some which are not as popular, such as imperative programming, declarative programming, event-driven programming, etc. If you are curious, there is a very good summary of programming paradigms here. In this post, we will discuss a popular one, which is Procedural Programming.

Tl; dr;

Procedural programming is one of the most popular programming paradigms out there. By following procedural programming, we split the code into smaller units called functions or procedures so that it is easier to reuse and the flow of execution is from top to bottom. This style is easy to understand and implement. As a result, it is often taught in introduction to programming courses.

Other blog posts on the series:

Programming paradigms – is object-oriented programming better?

What is Procedural Programming?

Procedural Programming diagram

Procedural programming is a method of the programming which has a support of splitting the functionalities into a number of procedures. In procedural programming, a large program is broken down into smaller manageable parts called procedures or functions. Here, priority is given on functions rather than data. In a procedural programming language, a program basically consists of a sequence of instructions each of which tells the computer to do something such as reading inputs from the user, doing the necessary calculation, displaying output.

def add_number(a, b):
    return a + b

# workflow starts here
# call function add_number() to add 1 and 2 together and print the result to screen
print(add_number(1, 2))
# execute add_number() function 5 times and print the result to screen
for i in range(5):
    print(add_number(i, 1))
# workflow ends here

When a program becomes larger, it is then broken into smaller units called procedure or functions. A number of functions are supposed to be written for the accomplishment of a specific task. These functions do not let code duplication because of the same function call from multiple locations. This technique is only suitable for medium sized software applications

In procedural programming, two types of data local and global are used. Data within the function are called local data and the data and which are not within any function are called global data. Local data are accessible to the only function where it is declared. So each function may access its local data as well as global data.

// this is a global variable
$globalVar = 1;

function addNumber($a, $b) {
    // this is a local variable
    $randomNumber = rand();

    return $globalVar + $randomNumber + $a + $b;
}

The local data of one function is not accessible to other functions. If any data is to be accessed by two or more functions it should be made global. However, global data are vulnerable to another programmer to be changed unknowingly. The separate arrangement of data and functions does a poor job of modelling things in the real world. That’s why procedural programming approach does not model a real-world system perfectly.

COBOL, FORTRAN, C, Pascal are some programming languages that can implement procedural programming.

Characteristics of procedural programming

A large program is broken down into small manageable procedures or functions. This reduces code duplication, which improves readability and maintainability of the code.

Different functions can share data via global variables. Functions are completely isolated, therefore if we want to share data, we need to declare it in the upper scope.

Functions can change global data. Since global data are transferred from function to function; during the course of the transformation, the global data may be altered in function chains.

Top-down methodology. As mentioned before, procedural programming follows the top-down methodology, workflow is going from top to bottom.

Advantages of procedural programming

  • The coding is easy and simple.
  • The codes have the ability to be reused in several parts of the program.
  • The programming paradigm consumes less memory on the computer.
  • It is easier for tracking the flow of the codes in the program written in the procedural programming paradigm.
  • The programming paradigm is regarded best for general programming to learn and implement.

Limitations or disadvantages of procedural programming

  • Focus on functions rather than data.
  • In large program, it is difficult to identify the belonging of global data.
  • The use of global data is error-prone and it could be an obstacle in case of maintenance and enhancements.
  • The modification of global data requires the modification of those functions using it.
  • Maintaining and enhancing program code is still difficult because of global data.
  • The procedural programming paradigm does not model the real-world problem very well since functions are action-oriented and do not really correspond to the elements of the problem.

Summary

Procedural programming certainly offers a lot of upside, one of them are easy to learn and implement. New programmers can start their journey by learning how to write code in procedural style. That will help them understand how certain programming language works.

However, due to its limitation, real world applications are not using procedural programming. Therefore students will need to learn other paradigms in order to get exposed to real life problems.

Thank you for reading. You can browse more posts about technology here.

By Tuan Nguyen

One thought to “Programming paradigms – what is procedural programming?”

Leave a Reply

Your email address will not be published. Required fields are marked *