What is a procedure?
Just like algorithmA sequence of logical instructions for carrying out a task. In computing, algorithms are needed to design computer programs., programSequences of instructions for a computer. are designed to solve a problem. In order to solve the problem, the program needs to perform several tasks. codeInstructions in a computer program. is written to perform each of these tasks.
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鈥檛 usually described, they just form part of the whole 鈥榖rushing teeth鈥 procedure. The next time you need to brush your teeth, you remember the 鈥榖rushing teeth鈥 procedure and know exactly what to do.
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 debugThe process of finding and correcting programming errors. . 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鈥檛 used, the code would have to be amended at every point that it appears in the program.