previous page main index next page
 

SECTION 2

Introduction

You've now covered some of the basic programming ideas and are now in a position to think in more detail about how best to make programs work for you. Solving problems can be a frustrating business but programming is meant to be fun too - remember that everyone makes mistakes and you shouldn't be surprised if your program doesn't work first time...

Procedures, procedures...

All of the programs you've done so far have been fairly short. For more complex tasks though, clearly, the code for the programs is going to be more complicated and much longer. For this reason, and also because it's good practice, most programs are broken down into smaller sections called procedures. A procedure, then, is a section of code which does a particular job. This part of the course is a rather simplified treatment of some of the issues involved in using procedures. We should note that, as with most things, there is more to the topic than meets the eye but a more detailed treatment can be left for later.

Example 19

To illustrate the use of procedures, we'll use Example 10, the design for which is shown below:

1.  get information from user
2.  calculate money raised
3.  print out results

...which led us to this:
 

1.1  ask user to enter flavor of chips
1.2  read in flavor
1.3  ask user for number sold on Friday
1.4  read in number sold on Friday
1.5  ask user for number sold on Saturday
1.6  read in number sold on Saturday
1.7  ask user for number sold on Sunday
1.8  read in number sold on Sunday

2.1  calculate total bags sold
2.2  calculate total money raised

3.1  print out flavor
3.2  print number of bags sold
3.3  print total money raised

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.
  • type in the code below
PROGRAM chips;         {to calculate chip sales}

VAR
  flavor   : STRING[10];
  friday   : INTEGER;
  saturday : INTEGER;
  sunday   : INTEGER;
  bags     : INTEGER;
  sales    : REAL;

BEGIN                   {main program starts here}
  get_info;
  calc_sales;
  results
END.

  • compile the program

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
1.2  read in flavor
1.3  ask user for number sold on Friday
1.4  read in number sold on Friday
1.5  ask user for number sold on Saturday
1.6  read in number sold on Saturday
1.7  ask user for number sold on Sunday
1.8  read in number sold on Sunday

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}
BEGIN
  WRITE('enter flavor of chips ');
  READLN(flavor);
  WRITE('enter number sold on Friday ');
  READLN(friday);
  WRITE('enter number sold on Saturday ');
  READLN(saturday);
  WRITE('enter number sold on Sunday ');
  READLN(sunday)
END;
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
  flavor   : STRING[10];
  friday   : INTEGER;
  saturday : INTEGER;
  sunday   : INTEGER;
  bags     : INTEGER;
  sales    : REAL;

PROCEDURE get_info;     {to get details from user}
BEGIN
  WRITE('enter flavor of chips ');
  READLN(flavor);
  WRITE('enter number sold on Friday ');
  READLN(friday);
  WRITE('enter number sold on Saturday ');
  READLN(saturday);
  WRITE('enter number sold on Sunday ');
  READLN(sunday)
END;

BEGIN                   {main program starts here}
  get_info;
  calc_sales;
  results
END.

  • type in the extra code and save it as chips2.pas
  • compile the program

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
2.2  calculate total money raised

...and the code is...
 

PROCEDURE calc_sales;   {to calculate the sales}
BEGIN
  bags := friday + saturday + sunday;
  sales := 0.65 * bags
END;
...and it slots into the program like this...
 
PROGRAM chips;         {to calculate chip sales}

VAR
  flavor   : STRING[10];
  friday   : INTEGER;
  saturday : INTEGER;
  sunday   : INTEGER;
  bags     : INTEGER;
  sales    : REAL;

PROCEDURE get_info;     {to get details from user}
BEGIN
  WRITE('enter flavor of chips ');
  READLN(flavor);
  WRITE('enter number sold on Friday ');
  READLN(friday);
  WRITE('enter number sold on Saturday ');
  READLN(saturday);
  WRITE('enter number sold on Sunday ');
  READLN(sunday)
END;

PROCEDURE calc_sales;   {to calculate the sales}
BEGIN
  bags := friday + saturday + sunday;
  sales := 0.65 * bags
END;

BEGIN                   {main program starts here}
  get_info;
  calc_sales;
  results
END.

  • it's your turn now to write the code for the procedure results and put it into the program
  • save the complete program
  • compile and run it and then show it to your teacher

Congratulations, you've just written your first Pascal procedure!
 

previous page main index next page
 
© 2001 by Mike Hardy