This time we've arrived at the concept of procedural programming in Pascal, where one complex problem gets broken into several simpler problems.
The problems that get broken down this way are called sub programs. Each sub program has a simple and specific job, and the jobs are then put back together into one complete program.
What Is Procedural Programming?
Procedural programming is one of the programming paradigms, where several statements are combined into one procedure to solve a specific problem. Instead of writing every instruction in a pile inside the main program, the work gets split into separate procedure modules.
๐ท Illustration: a block diagram of the main program flow, split into calls to several independent procedure modules.
A simple everyday example of a procedure is making bread with jam. It goes roughly like this.
- Prepare the jam and the sliced bread.
- Spread the jam on the bread and even it out.
- The jam sandwich is ready to eat.
Simple, only 3 steps. In practice it won't be that simple, though. The real thing probably looks like this.
- Go to the supermarket and buy bread and jam.
- Go back home.
- Open the bread wrapper and the jam jar.
- Put the bread on a tray.
- Take a spoon or a bread knife.
- Take the jam with the spoon or the bread knife.
- Spread the jam on the bread.
- Even out the jam.
- Close the jam jar and the bread wrapper.
- Pick up the jam sandwich with your hand.
- Open your mouth.
- Put the bread into your mouth.
Wow, that's long ๐
The 3 steps mentioned at the start should be enough to make a jam sandwich. The 12 steps after them are only the breakdown.
If those 12 steps are grouped by their specific purpose, you end up with the 3-step procedure mentioned at the start.
Imagine if every recipe book spelled out the long, specific steps like that. History books would end up looking thin next to them. Code is the same way: it can get so long that reading it becomes hard.
With procedural programming, long sequences of steps get grouped by purpose, so the code is easier to read.
Procedural Programming in Pascal
Pascal is one of the procedural programming languages. As a procedural language, Pascal has a way to break complex problems down into simple ones inside several sub programs.
A sub program in Pascal can be a PROCEDURE or a FUNCTION.
Procedure
A PROCEDURE is a sub program in Pascal that runs a set of steps without returning a value.
Some examples of PROCEDUREs we already use a lot are CLRSCR, WRITELN, READLN, WRITE, and READ.
Those procedures look simple (just input and output), but plenty of operations happen behind the screen.
We'll go deeper into them next time.
Function
Next there is FUNCTION. This sub program is like a Procedure, but it can return a value, so it can be used in an assignment.
FUNCTIONs that Pascal already ships with include ABS for absolute value, SQRT for the square root of a number, and SQR for squaring a number.
// x holds 2 (the square root of 4 is 2)
x := sqrt(4);
// y holds 9 (|-9| = 9)
y := abs(-9);
We'll go deeper into them another time.
Wrapping Up
That's enough of an introduction to procedural programming in Pascal. Hope it helps ๐
Next time we'll look further into PROCEDURE in Pascal.
See ya...