Lesson 5 -- Procedures and Functions

PROCEDURES and FUNCTIONS are specific named sections of your code. By breaking down your code into smaller sections, each performing a specific task, you make your code easier to manage. Think of PROCEDURES and FUNCTIONS as mini-programs. The syntax for a PROCEDURE is:

ex5-1.gif (3306 bytes)
figure 5-1

You must start your PROCEDURES with the PROCEDURE keyword. Next it must have a name that doesn't appear in another PROCEDURE or FUNCTION. Note that DCAL has many internal PROCEDURES and FUNCTIONS and you should not use any of their names either.

A PROCEDURE may take parameters (<optional_params>) These parameters are variables that will be used in some way by the PROCEDURE. Your PROCEDURES may declare variables that they need to do their work.

Variables used only in and seen only within a PROCEDURE are called local variables. The use of local variables is optional. The keyword BEGIN tells the compiler here is where the actual work starts.

Finally the PROCEDURE ends with the keyword END followed by the name of the procedure. The name must match at both the PROCEDURE declaration and the END of the PROCEDURE.

The syntax for a FUNCTION is:

 ex5-2.gif (3758 bytes)
figure 5-2

A FUNCTION is similar to a PROCEDURE. It must have a unique name <function_name>. It begins with the FUNCTION keyword and ends with the END keyword followed by the name of the FUNCTION. It may declare local variables and it needs the BEGIN keyword before the <statement(s)> section. A FUNCTION may take parameters.

The key difference is that a FUNCTION will return a value. What this means is that a FUNCTION will evaluate to one of DCALs TYPES. A FUNCTION may have multiple RETURN statements in its code however only the first one that is encountered will be executed. Once the program hits a RETURN statement the <return_value> is sent back to the routine that called the FUNCTION and the FUNCTION terminates. The difference between PROCEDURES and FUNCTIONS are subtle but important.

< Previous Lesson  |   Next Lesson >
DCAL Tutorial Contents



SITE SEARCH