Get startedGet started for free

When is procedural programming used?

1. When is procedural programming used?

Now that we've got a basic understanding of what procedural programming is, it's time to look at some examples of when it is used. Specifically, we want to look at typical problems that can be solved with procedural programming, some pros and cons of working within this paradigm, and talk a little bit more about how it relates to the broader category of imperative programming. Let's dive in!

2. Typical applications for procedural programming

Procedural programming is a commonly used paradigm in general-purpose programming languages like Python. This approach makes sense when the problems we are trying to solve naturally break down into coherent, potentially repeated steps. It is great for automating processes such as importing, cleaning, and exporting data for analysis. Procedural programming is also used in building websites, and in any kind of coding task where the problem to be solved can be defined step by step and then broken up into smaller tasks.

3. Procedural programming example

Here is an example of what a procedural program can look like. Imagine that we have the raw price in cents of an item in our store saved into a variable called price_in_cents and we want to be able to print to the screen what the customer's final price will be, in dollars, and after applying a discount. The program above does exactly that. First, we take our price in cents and divide by 100 to get the price in dollars. Next, we apply the 20% discount to get a final price. Finally, we print the result to the screen.

4. Pros and cons of procedural programming

Given how commonly used and widely applicable procedural programming is, it may be surprising to consider that it isn't the right paradigm for all situations. Let's compare some of the pros and cons of this paradigm. Some of the advantages of procedural programming that we've already talked about are that it enables and encourages writing modular code, which can save development time and effort. Procedural programming is also relatively simple to use, making it more accessible to beginners than other paradigms, and this is aided by the fact that there are a lot of educational resources available covering programming in this paradigm. Finally, as we've seen, it is a paradigm that is appropriate for a wide range of use cases. On the flip side, procedural programming is not always the best choice. Procedural programs can be less secure than programs written using some other paradigms because the data is visible through the entire program. It is also the case that while procedures within a program are reusable for that same project, it is rarely the case that procedural code will be reusable for other projects, which isn't the case with all paradigms. Finally, since procedural programming places a focus on the operations of the program (what the program is doing, step-by-step) rather than the integrity of the data itself in the program, it isn't always the best choice.

5. Procedural programming vs. imperative programming

Before we move onto some exercises, let's take one final look at what it means that procedural programming is an imperative programming paradigm, but not all imperative programming is procedural programming. All imperative programming makes use of step-by-step instructions to tell the program exactly how to execute. The key difference is that procedural programming specifically makes use of procedures (or subroutines) to organize the code and dictate the flow of the program.

6. Let's practice!

Now that you've seen some of the use cases for procedural programming and its advantages and disadvantages, let's put that knowledge to the test with some exercises!