1.1 ask user to enter flavor of chips
2.1 calculate total bags sold
3.1 print out flavor
|
Notice that, as with many designs, we start with a basic scheme with a small number of steps - in this case, three - and then take each of them in turn and write a more detailed description. We wish to split our program into smaller 'chunks', or procedures, so we'll decide now that there will be three - one for each of the main steps 1, 2 and 3. Have a look at what each one does then decide on a name for the procedure in the same way that you choose names for variables.
|
step | name |
1. get information from user | get_info |
2. calculate money raised | calc_sales |
3. print results | results |
The main program now consists of the three procedures.
|
PROGRAM chips; {to calculate chip sales} VAR
BEGIN
{main program starts here}
|
The error message that you get isn't really surprising since Pascal knows nothing of your three procedures! We need to describe what each procedure does. Let's take get_info first and remind ourselves of the detailed description for step 1 as given in Example 10 1.1 ask user to enter flavor of chips
Our procedure will do just what is required for each of these steps. The code is shown below.
|
PROCEDURE get_info;
{to get details from user}
|
Notice again how closely the instructions follow the detailed design. The code for the procedure is slotted in before the start of the main program so that we now have:
|
PROGRAM chips; {to calculate chip sales} VAR
PROCEDURE get_info;
{to get details from user}
BEGIN
{main program starts here}
|
Again the error message should come as no surprise since we have only entered one of the three procedures so far. Step 2 looked like this and the procedure only has a couple of instructions in it... 2.1 calculate total bags sold
...and the code is...
|
PROCEDURE calc_sales;
{to calculate the sales}
|
...and it slots into the program like this...
|
PROGRAM chips; {to calculate chip sales} VAR
PROCEDURE get_info;
{to get details from user}
PROCEDURE calc_sales;
{to calculate the sales}
BEGIN
{main program starts here}
|
Congratulations, you've just written your first Pascal procedure!
|
© 1998 John Brewer |