Lesson 5 -- Procedure & Function Examples

In order to be sure you understand the difference between Procedures and Functions here is a sample macro:

ex5-3.gif (15004 bytes)
figure 5-3

Complete listing for PROFUNC.DCS.  Click the image to download the source code. If your browser prompts you to run the file or save it to a disk, select save to disk. If you create a file association for *.DCS files this file will automatically be opened in your editor.

The sample above can be compiled and modified to study PROCEDURES and FUNCTIONS. No fancy linking is required just save it as PROFUNC.DCS and compile and link it.

Hopefully portions of this are starting to make sense. First we setup the macro as a PROGRAM and declare its name PROFUNC. Next we have our first PROCEDURE. This is about as simple as a PROCEDURE can get. It has no parameters or variables. It uses the DCAL built-in PROCEDURE WRTERR to display a message on the error line. That's it. Next we see a simple FUNCTION. This function takes two integers (int2 & int2) as its parameters. It will return an integer as evidenced by the ':integer' after the parameters. This function uses no local variables. The result of adding int1 and int2 are calculated and this result is RETURNed to the calling routine.

The last PROCEDURE is the one that does all the work. This is a fairly typical PROCEDURE for setting up a menu, waiting for the user to select an option and then acting on that option. Notice the use of the REPEAT loop that was covered yesterday. For more information on how this menu loop works refer to yesterdays tip.

First we show the menu buttons then wait until the user picks one of the keys we have displayed. Once we have a key we check which key was pressed with an IF statement. If the user pressed F1 we call our show_message PROCEDURE. If F2 was pressed we call our add2ints FUNCTION. The value that is RETURNed from the add2ints FUNCTION is assigned to result. If you call a FUNCTION you must do something with the result that is RETURNed. In this case we simply store the value that is RETURNed in the variable result. The integer value in result is then converted to a string by way of the CVINTST PROCEDURE that is built-in to DCAL. Lastly for this menu selection, the string is displayed on the error line. Finally if the user selected the S0 key the loop exits and the macro ends.

You are not required to use PROCEDURES or FUNCTIONS in your code. You should use them to break down the code into small manageable chunks that each do a specific task. Try to limit each PROCEDURE or FUNCTION to one specific task. The show_message and add2ints examples were very specific in that they each did just one thing. Remember that this was only an example. Obviously the code contained within show_message could have been placed in the main PROCEDURE so that it was only executed once the user selected F1. This is an extreme example. You don't want to write a separate procedure in 4-5 lines of code if the same thing can be accomplished in 1 line elsewhere. Only you can determine when a PROCEDURE or FUNCTION is necessary.

< Previous Lesson   |   Next Lesson >
DCAL Tutorial Contents



Thank you for printing this page. Please feel free to contact us for further assistance. You can call our sales department at +1 (800) 394-2231, Monday through Friday from 8:00 a.m. to 5:00 p.m. Eastern time or send an e-mail message to info@datacad.com.