You use procedures in life every day, even if you don't think about it. From making breakfast to requesting that brand new PC you've always wanted, there's a procedure to follow. A specific set of actions you take to get it done. The whole idea of a procedure is to break down a task into smaller, more manageable tasks. Let's look at making some chocolate ice cream. What are the tasks you would need to perform?
Beat egg yolks lightly
Beat in sugar
Heat the cream/milk on the stove
Beat in cocoa powder
Heat cream/milk/cocoa mix until steaming
Stir into egg/sugar mix
Add vanilla extract
Freeze in ice cream maker
This is the procedure we would follow to make basic chocolate ice cream.
Now we have defined this set of instructions, we don't need to write them out again. We can just refer to the recipe by its name . So what's this got to do with programming? Well, in programming you can do the same sort of thing. You can group together a set of instructions under a single name, and then use this name when you want to run those instructions. This is a good thing for several reasons:
It allows us to break complex problems into smaller, more discrete tasks. This can often make the problem easier to solve.
It makes your code smaller, because you only need the instructions entered once.
It makes your code easier to maintain, because if there is an error in the instructions, you only need to correct it in one place.
It makes it easy to use the instructions in other applications.
You can change the inner workings of your procedure without worrying about others who use it. As long as it has the same result, no one will know the difference.
You may not think this is a big issue, but as your programs start to get more complex you'll find that dealing with procedures is much easier than having a single large chunk of code. The last point in the list is one you'll see mentioned again later in the book, as it means you can change the internal workings of a procedure, perhaps to improve performance, without changing the end result of the procedure. Think about our 'making ice cream' example. If you make ice cream by hand, you don't use Step 9 above. There may be two or three steps here instead, describing how long you should place the ice cream in the freezer for, and how often you should stir it. But if you buy an ice cream making machine, you could replace these steps with Step 9, and the end result would be the same.