PROCEDURES

Procedures are collections of Comal code which can be used anywhere in a Comal program. They are defined by the programmer, and the procedure is given a unique name. They are essential components in a structured program, and make it much easier to plan and implement a complex program. They also make it easier to trouble-shoot any problems encountered.


Procedure Name

All procedure names follow most of the same rules as variable names, with the important exception of the "$". Procedure names never take the "$" suffix.


Procedure Declaration

The Declaration is the definition of the procedure. All procedure declarations start with the keyword PROC, followed by the name you have chosen to use for this procedure, e.g.

  PROC add_numbers  or
  PROC get_name  etc.

The code you then require is inserted using normal Comal rules, and the declaration is finished by using the keyword ENDPROC followed by the same procedure name e.g.

  ENDPROC add_numbers  or
  ENDPROC get_name  etc.

Once you have declared the procedure, you are free to use it in your program. When you wish to use your declared procedure, you call the procedure by entering the procedure name in your code.

0010 // Name
0020 //ver 1.0
0030 //add two numbers with procedures
0040 //06/09/99
0050 //
0060 //
0070 title //calling procedure for setting up title
0080 get_number //calling procedure for asking,input and confirmation of num
0090 print_answer //calling procedure for producing the answer
0100 END //stops the program
0110 //
0120 //
0130 PROC title //procedure declaration
0140 PRINT AT 10,25: "Adding two numbers using procedures and repeat"
0150 PRINT
0160 PRINT AT 20,25: "You will be asked to enter two numbers"
0170 PRINT AT 25,25: "When you are ready to start, press the space_bar"
0180 PAUSE //pauses the program until a key is pressed
0190 PAGE //clears the screen
0200 ENDPROC title //end of the procedure declaration
0210 //
0220 PROC get_number //procedure declaration
0230 count:=0 //initialising the new variable
0240 REPEAT //starting the loop
0250
0260 PRINT "Please enter a number "
0270 PRINT
0280 INPUT num //users number stored as value in variable num
0290 answer:=answer+num //variable answer used as an accumulating total
0300 count:=count+1 //variable count uded as incrementing total (counter)
0310 PRINT
0320 PRINT "You have entered number ";num //confirmation of entered value
0330 PRINT
0340 UNTIL count=2 //condition set to stop loop
0350
0360 ENDPROC get_number //end of procedure declaration
0370 //
0380 PROC print_answer //procedure declaration
0390 PRINT "The sum of the two numbers is ";answer //print the answer
0400 PRINT
0410 PRINT "Goodbye"
0420 ENDPROC print_answer //end of procedure declaration


As you can see from this simple program, three procedures are used.

  1. title
  2. get_number
  3. print_answer
The procedure title is common to most programs, and is designed to give the user the basic information and instructions on the program being run.

The procedure get_number does a number of things. It contains a loop, so that it saves writing a lot of duplicate code; it gives the user a clear instruction; it accepts user input; it verifies the number input; it counts how many times the loop has run, it works out the answer to the problem.

The procedure print_answer displays the answer to the problem and tells the user that the program is finished.

If we had wanted the user to have two attempts at this adding program, because of the use of procedures it is very simply done. Instead of writing all of the code a second time, the two procedures, get_number and print_answer would be called again, see below:

0010 // Name
0020 //ver 1.1(Note change in version number!)
0030 //add two numbers with procedures, twice
0040 //06/09/99
0050 //
0060 //
0070 title //calling procedure for setting up title
0080 get_number //calling procedure for asking,input and confirmation of num
0090 print_answer //calling procedure for producing the answer
0100 get_number//calling procedure for asking,input and confirmation of num
0110 print_answer//calling procedure for producing the answer
0120 END //stops the program

Each time the procedure is called, all of the code you have declared is run again.

NOTE: Procedure names (just like variable names) should be descriptive, and MUST NOT use the same name as a variable. If you plan your program properly, then the procedure names are almost self evident.