A Rexx program consists of one or more procedures. There are three types of procedures.
Main procedures | these are programs that are started as commands |
Internal procedure | internal routines that are called or invoked as functions |
External procedure | external routines that are called or invoked as functions |
The following diagrams show variations of these procedure types.
Main.rex, Main2.rex, and Main3.rex are main programs.
The Main2.rex source file includes an internal procedure named: A.
The Main3.rex program invokes an external procedure named: EXT.
Note: the naming convention that is used for Rexx source files is implementation-dependent. Within this tutorial, Rexx source files are identified using a .rex file extension, as though the file were defined in a traditional IBM PC file system. Other file extensions are used instead. Other environments, such as IBM's VM operating systems, use different naming methods. |
Insight!
When you study the above diagram you might believe that a Rexx program consists of a main program,
and optionally a series of internal procedures. This is the approach that is used by the majority
of programming languages.
There are additional flow possibilities within a Rexx program.
All of the labels within the source file are eligible targets
for the signal instruction (an analogue of a goto),
even if these labels are beyond an internal procedure.
Note, labels that are followed by a procedure
instruction should not be used as targets of a signal instruction.
A simple Rexx program consists of a main procedure only. All of the instructions within the program's source file are included in the main procedure.
/* sayit.rex -- this program says it all */
interpret say arg(1) |
As brief as the above program appears, it is actually a full function calculator! It can perform arithmetic computations and string processing requests. The power of this program is due to the INTERPRET instruction.
A more complicated Rexx program has a main procedure, but uses an internal procedure as well. The main procedure is concluded by a RETURN or EXIT instruction. The internal procedure is invoked by a CALL instruction, or by a function request. The internal procedure begins with an optional PROCEDURE instruction. The internal procedure is normally concluded by a RETURN instruction. When the procedure is invoked as a function, the RETURN instruction must provide a result. When the procedure is invoked by a CALL instruction the result of the RETURN instruction is available in the RESULT special variable.
Internal procedures can CALL or invoke other procedures as functions. These other procedures can be either internal or external.
The following is an example of a main program that uses an internal procedure.
/* factorialProgram.rex -- computes the factorial of a number */ /* Usage: exec factorialProgram number */ /* Example: exec factorialProgram 5 */ arg N . call factorial N say result exit 0 /* don't fall through to the PROCEDURE instruction */ /* internal procedure FACTORIAL * returns factorial of argument N */ factorial : PROCEDURE n = arg( 1 ) if n = 1 then return 1 return n * factorial( n - 1 ) |
If an EXIT instruction is executed in an internal procedure, then the main program is concluded as well!
An external procedure is similar to a combination of a main procedure and an internal procedure. An external procedure is stored as a separate source file. Unlike an internal procedure, an external procedure does not begin with a PROCEDURE instruction. Thus it begins with a series of instructions similar to a main procedure. The external procedure is invoked using the same syntax as an internal procedure. The external procedure is normally concluded by a RETURN or EXIT instruction. When the procedure is invoked as a function, the RETURN or EXIT instruction must provide a result. When the procedure is invoked by a CALL instruction the result of the procedure is available in the RESULT special variable.
The external procedure can invoke procedures that are internal to its source file. The external procedure can invoke other external procedures.
If an EXIT instruction is executed in any procedure within the context of an external procedure, then control is returned to the procedure that invoked the external procedure.
The method that is used to locate and activate an external procedure is implementation-dependent.
Note: Some implementations allow external procedures to be written in other programming languages.
/* factorialProgram2.rex -- computes the factorial of a number */ arg N . say factorial( N ) /* this invokes the external procedure */ |
The external procedure, factorial.rex, is defined in a separate source file !
/* factorial.rex -- computes the factorial of a number */ n = arg( 1 ) if n = 1 then return 1 return n * factorial( n - 1 ) |
The processing state of the following information is saved when a function or subroutine call is performed. The state of this information is restored when the function or subroutine returns.