Programming Concepts: Procedure-oriented programming

UNIT 3 - ⇑ Fundamentals of programming ⇑

Procedure-oriented programming Object-oriented programming →

Overview edit

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 edit

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:

i1 = input( "First number" )
i2 = input( "Second number" )
result1 = i1 + i2

i3 = input( "First number" )
i4 = input( "Second number" )
result2 = i3 + i4

i5 = input( "First number" )
i6 = input( "Second number" )
result3 = i5 + i6

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

result1 = sum()
result2 = sum()
result3 = sum()

def sum():
	i1 = input( "First number" )
	i2 = input( "Second number" )
	return i1 + i2;

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

Benefits of Procedure-oriented programming edit

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 sum 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 sum procedure.

Background edit

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.