A-level Computing/AQA/Paper 1/Fundamentals of programming/Procedure-oriented programming





Overview
Ordinary programs execute line after line until they reach the end. This is very simple to read through, but as programs become longer it is difficult to read, difficult to maintain, and bugs become more likely.

Procedure-oriented programming is a way of imposing structure onto a program - specifically: adding procedures - so that it is easier to work with.

Example
Almost all programs use procedures to some extent. For instance, we need procedures to display output on the screen, or to read input from the user. But in Procedure-oriented programming, we also create new, customised, procedures as much as possible, to simplify the program itself.

Here's a program that is not procedure-oriented. It uses the "input" procedure, but repeats almost identical lines of code three times:

Here's the same example using Procedure-oriented Programming:

When a procedural program encounters a procedural name such as  on line 1, then the program jumps off, executes the code corresponding to   and returns to execute line 2.

Benefits of Procedure-oriented programming
Adding procedures to a program has several benefits:


 * Allows us to re-use code without re-typing it (reduces the time to write it)
 * Allows us to re-use code that works (each time we re-write code, we have a chance of accidentally adding a bug)
 * Makes it clearer what the program does and how (each procedure needs a name; the name explains what those lines of code are for)
 * Reduces the length of the program (makes it easier and faster to read and debug)
 * Allows compilers to make more optimizations (compilers detect the re-used code and use this to take shortcuts)

In the example above, the new procedure  asked the user for two numbers, calculated the sum, and returned it. The name of the procedure enabled us to read the code and make intelligent guesses even without reading the contents of the  procedure.

Background
If a program only requires a few lines of source code to write down, there is little or no advantage to using this technique.

In particular, if none of your code is repeated, and there are no sections of inter-related code (eg. the three lines of code for summing two numbers above), then adding a procedure can make the program longer and harder to work with. Unnecessary procedures do not generally improve the program.

Procedure-oriented and Object-oriented programming were invented because programs were getting longer and longer, and were difficult to work with. Programmers needed more structure to simplify the programming process.

A program of moderate size and complexity can be simplified using procedures.