What is a procedure?

Just like algorithms, programs are designed to solve a problem. In order to solve the problem, the program needs to perform several tasks. Code is written to perform each of these tasks.

curriculum-key-fact
A procedure is a small section of a program that performs a specific task. Procedures can be used repeatedly throughout a program.

A real-life example of a procedure is brushing your teeth. There are several steps involved in this process – getting your toothbrush, squeezing the toothpaste onto the brush, brushing all your teeth, rinsing your mouth out and putting everything back. Each of these steps isn’t usually described, they just form part of the whole ‘brushing teeth’ procedure. The next time you need to brush your teeth, you remember the ‘brushing teeth’ procedure and know exactly what to do.

Brushing your teeth can be broken down into multiple steps. Pick up your toothbrush, put toothpaste onto brush, brush teeth, rinse mouth, rinse toothbrush and put tooth brush back.

Why use procedures?

Program code is easier to read and understand when it is broken up into smaller sections. By breaking a program up into these sections, or procedures, code can be made shorter and simpler.

When writing programs it is very easy to end up with long-winded, repetitive code which is hard to understand and debug. This is because the same task often needs to be carried out in different places in the program. Procedures can be used throughout a program, making them simpler and quicker to code.

Using procedures has an added benefit. If something needs to be changed in a procedure, it only needs to be changed once, within the procedure code. This change will then appear wherever the procedure is used in the program. If procedures weren’t used, the code would have to be amended at every point that it appears in the program.