1. What is procedural programming?
Let's take a look at one of the most popular programming paradigms: procedural programming. We're going to start by answering the most basic question: what is procedural programming?
2. What is procedural programming?
Before we get into any details about how and when to use procedural programming, let's first introduce a definition of this paradigm. Procedural programming is an imperative programming paradigm which is based on the concept of the procedure. A procedure (also sometimes called a subroutine) is a series of steps defined in a subsection of code that can be referenced elsewhere and rerun multiple times. It is important to note that procedural programming is a subset of imperative programming, and not all imperative programming is procedural, although the terms are sometimes (incorrectly) used interchangeably.
3. Procedures in procedural programming
Let's look a little more closely at what procedures are in procedural programming. Procedures are how this paradigm achieves separation of responsibilities, and therefore aid with modularity of code. This is because defining a procedure allows for it to be called and rerun in multiple places typically with just one line of code, no matter how long the procedure itself is. Further, defining and calling procedures helps with organization and readability. It is possible to read through the main section of code, see another procedure referenced by name and know roughly what it is doing without needing to look into the detail.
4. Example of procedures
Let's look at a quick example of procedures in action. In Python, procedures are generally implemented as Python functions. Imagine for example that we are reading through some Python code and come across the reference to a sort_ducks procedure. Since this is well-named, we can probably assume what it is doing (sorting ducks) without needing to look at its definition. However, if we wanted to, we can take a look inside the definition to see how it is performed. We can then reference this sort_ducks function anywhere else in the code where we have some ducks we need to sort.
5. Example of a procedural program
Let's take a look at this longer Python example featuring a procedure. The procedure here is called print_initial and is pretty simple: it takes a person's name as input and prints the first letter of it (the person's initial). To demonstrate how this saves space, we have defined it once and called it three times, on each of our three friends' names: Marwa, Celia, and Raqael. The output for these three calls will be "M", "C", and "R". Imagine how much space this would save if we had to do it 100 times, or if we needed to do something more complicated than printing the initial!
6. Let's practice!
Super! Now that we've got the basics of procedural programming down, let's test that knowledge with some exercises.