Procedures and Functions |
The most powerful compiler structures are PROCEDURES and FUNCTIONS. In effect, procedures allow you to create your own “instructions” from a sequence of other RATS commands, while functions define callable functions similar to built-in functions like INV and %DENSITY.
We include hundreds of procedures with RATS, and updates of those, plus other new ones are available for downloading from our web site and other sources (Help—Update Procedures can help maintain your collection of procedures.) These allow you to perform many complex tasks without having to code them yourselves. In the following pages, we’ll tell you how to make use of these existing procedures. Then, we’ll discuss the various elements of RATS procedures and guide you through the process of writing a complete procedure so you can get a feel for how it’s done. We’ll focus mainly on procedures—functions are similar but can’t use all the features of a procedure. We’ll often use the word subprogram when discussing information that applies to both procedures and functions.
First, a look at the basic elements of a procedure: A procedure begins with a PROCEDURE statement and ends with a matching END. A procedure may have some, all, or none of the following special features:
•Parameters, the names of which are listed following the procedure name on the PROCEDURE statement. Use these to pass information to the procedure. Their types are set using the statement TYPE.
•Local variables, recognized only within the procedure. They are set up by the statement LOCAL.
•Options, which operate much like those for a standard RATS instruction and are set up using the statement OPTION.
•Supplementary cards, which are defined with the instruction ENTER.
A function begins with FUNCTION, ends with a matching END, and can include parameters and local variables, but not options and supplementary cards.
Comparison With Other Structures
Unlike other types of compiled structures, these are not executed automatically when the processing of its code is finished. Instead, RATS saves the generated code internally and only executes it when it receives an EXECUTE instruction (or the @procedure shortcut) for a procedure, or sees function name(arguments) for a function. They can be invoked any number of times, again differing from the other structures, which execute only once.
Because a subprogram is not a loop, you cannot use BREAK to leave it before the end. Use RETURN instead. Also, if you have calls to a subprogram nested inside another subprogram, you must compile the code for the inner subprogram first, so that RATS will recognize the procedure or function call when you compile the main routine.
Copyright © 2025 Thomas A. Doan