Procedural languages
Introduction
Procedural languages are sometimes called 'imperative' languages. This comes from English grammar! The imperative form of a verb is where you give a command. For example, in the verb 'to stand', the imperative form of the verb is 'Stand'. It's a command to tell someone to stand up. An imperative language is therefore one where the instructions in the language are simply commands that tell the program exactly what to do next.
Compare an imperative language with an example a fourth generation language, such as SQL:
CREATE table Dogs
SELECT * FROM Dogs
This tells the program to create a table called Dogs and then select all the pieces of information in the table. What it doesn't do is what a procedural language would have to do; it doesn't say how you create the table and how you are going to read all the contents from the table and where you are going to store the data whilst you are using it.
Characteristics of a procedural language
These types of languages are third generation languages and examples include BASIC and Pascal. Programs are laid out using three control structures. These are sequence, selection and iteration (repetition). In addition, programs are typically broken down into logical blocks, each of which carries out one task. These are known by a variety of names, including function, procedure, subroutine, module and unit. Programs make use of variable names, which are just real words that describe memory locations holding data. Whilst it might not seem that important, it actually makes the whole program much easier to read, understand and importantly, debug (find any errors in the code).
Control structures
When you look at any high-level language, you can identify the features in the language that make it so powerful. Instructions in a program flow one after another in a sequence. However, there are instructions available that will allow blocks of code to be repeated many times. There are also instructions that allow decisions to be made about which code from a selection of code will be executed. The specific instructions provided by a particular language that allow code to be repeated, or code to be selected from a choice of code, are known as control structures. If we also include the way that instructions flow one after the other, we can say that there are three types of programming construct we need to know about. These we will call:
- Sequence.
- Selection.
- Iteration, also called repetition.
Sequence
There isn't a lot to say about the programming construction 'sequence'. It simply describes one instruction following on from the next instruction. An example of this, in pseudo-code, is:
INITIALISE Variables
WRITE ‘Please enter the name of student’
INPUT Name
WRITE ‘Please enter the exam mark of student’
INPUT ExamMark
PRINT Name, ExamMark
Selection
A selection construct looks at some data held in a variable and tests it. If the result of the test is TRUE, we do one block of code. If the result of the test is FALSE, we do a different block of code. In this way, we can alter the program flow and execution. Some blocks of code may never get executed at all whilst others will get executed.
Iteration
Iterative constructs (also known as repetitive constructs) repeat the same block of code a number of times. Sometimes, an iterative instruction will say how many times a particular block of code should be repeated. Other types of iterative instructions will tell a program to repeat a block of code constantly, but before the code is run again, a test is carried out. The code is run only if the test result is TRUE. Eventually, the test returns a FALSE and the block of code isn't done any more.
BASIC for Windows
You can quickly get some experience writing procedural programs in BASIC. You will need to download and set-up BASIC on your computer here:
http://www.bbcbasic.co.uk/bbcwin/download.html
and then work through the outstanding Beginners' Tutorial here:
http://www.bbcbasic.co.uk/bbcwin/tutorial/index.html
It can take just a few hours to get a really good idea of what procedural programming is all about and to start to use and understand the features and structures that underpin this kind of language.