PROCEDURE Instruction |
PROCEDURE procname parameters
LOCAL, OPTION, TYPE instructions if any
executable instructions
END
The PROCEDURE instruction begins the definition of a RATS procedure. It specifies the name of the procedure and the parameter list, if any.
Parameters
|
procname |
The name you want to give this procedure. procname must be distinct from any other procedure or variable name in the program. |
|
parameters |
Names of the formal parameters. These names are local to the procedure—they will not conflict with variables elsewhere in the program. When you execute the procedure, RATS passes the values on the EXECUTE instruction to the formal parameters.
By default, parameters are INTEGER passed by value. Use TYPE to use other RATS Data types or to pass by address. |
Description
The most powerful compiler structure in RATS is the procedure. A procedure is similar to subroutines of Fortran, the functions of C or the procedures of Java. In effect, procedures allow you to define new instructions from a sequence of RATS commands.
A procedure begins with a PROCEDURE statement and ends with a matching END. The PROCEDURE statement itself names the procedure and lists the formal parameters. The usual arrangement of the statements in a procedure is
procedure statement
type, declare, local, option and fixed statements, if any.
other instructions
end
You should try to write the procedure using only parameters, options, local variables and global variables which RATS itself defines. That way you don’t have to worry about accidentally changing a global variable. If you have a global variable that you want to access outside the procedure, use a name starting with %% so it won’t conflict with either the user’s names or any names defined by RATS.
An alternative to a procedure is a function, which you can create with the FUNCTION instruction. Where procedures define new instructions, functions define operations similar to the functions used within RATS expressions.
Examples (Partial)
procedure cumpdgm series start end
type series series
The procedure CUMPDGM has three parameters: SERIES is a type SERIES, START and END are INTEGER.
procedure distrib oldser newser
type series oldser *newser
option real rho .9
option integer factor 3
option choice model 1 rw1 ar1 rwar1 rw2
DISTRIB takes two parameters: both are series, but OLDSER is input to the procedure and NEWSER is output by it. The * means that the NEWSER parameter is passed by address, and so its value can be set or changed by the procedure.
Using SOURCE
Once you have a procedure working the way you want, it is usually a good idea to save it as a separate file, so you can use it in different applications. A well-designed procedure can be used with a variety of data sets if specific information about the current data is passed to the procedure through parameters and options, rather than being hard-coded into the procedure. It’s most useful to name it as procname.src which will allow RATS to locate it automatically when you use it.
If you have a procedure on a separate file, bring it into your current RATS program using the instruction SOURCE. The typical instruction is
source file with procedure
If you have a collection of procedures which you use regularly, you can create a procedure library that gets brought in right at the start of your program.
Running a Procedure
Procedures are executed using the EXECUTE command or (more commonly), using the @ sign (a shortcut for EXECUTE). For example:
@cumpdgm x 1980:1 2013:12
Order of Procedures
For complex tasks, it is very common for a procedure to execute other procedures. Because RATS needs to know the syntax of a procedure before it can even interpret the instruction which will execute it, it must process the sub-procedure before the main one. Thus you either need to place the sub-procedure first in your file, or “SOURCE” it in from a separate file before processing the main procedure.
Copyright © 2026 Thomas A. Doan