Rexx - Basic Syntax


Advertisements

In order to understand the basic syntax of Rexx, let us first look at a simple Hello World program.

Example

/* Main program */ 
say "Hello World" 

One can see how simple the hello world program is. It is a simple script line which is used to execute the Hello World program.

The following things need to be noted about the above program −

  • The say command is used to output a value to the console.

  • The /* */ is used for comments in Rexx.

The output of the above program will be −

Hello World

General Form of a Statement

In Rexx, let’s see a general form of a program. Take a look at the following example.

/* Main program */ 
say add(5,6) 
exit 
add: 
parse arg a,b 
return a + b

The output of the above program will be −

11

Let’s go through what we have understood from the above program −

  • Add is a function defined to add 2 numbers.

  • In the main program, the values of 5 and 6 is used as parameters to the add function.

  • The exit keyword is used to exit from the main program. This is used to differentiate the main program from the add function.

  • The add function is differentiated with the ‘:’ symbol.

  • The parse statement is used to parse the incoming arguments.

  • Finally, the return statement is used to return the sum of the numeric values.

Subroutines and Functions

In Rexx, the code is normally divided into subroutines and functions. Subroutines and functions are used to differentiate the code into different logical units. The key difference between subroutines and functions is that functions return a value whereas subroutines don’t.

Below is a key difference example between a subroutine and a function for an addition implementation −

Function Implementation

/* Main program */ 
say add(5,6) 
exit 
add: 
parse arg a,b 
return a + b

Subroutine Implementation

/* Main program */ 
add(5,6) 
exit 
add: 
parse arg a,b 
say a + b

The output of both the programs will be the value 11.

Executing Commands

Rexx can be used as a control language for a variety of command-based systems. The way that Rexx executes commands in these systems is as follows. When Rexx encounters a program line which is neither an instruction nor an assignment, it treats that line as a string expression which is to be evaluated and then passed to the environment.

An example is as follows −

Example

/* Main program */ 
parse arg command 
command "file1" 
command "file2" 
command "file3" 
exit 

Each of the three similar lines in this program is a string expression which adds the name of a file (contained in the string constants) to the name of a command (given as a parameter). The resulting string is passed to the environment to be executed as a command. When the command has finished, the variable "rc" is set to the exit code of the command.

The output of the above program is as follows −

sh: file1: command not found
     3 *-* command "file1" 
       >>>   " file1"
       +++   "RC(127)"
sh: file2: command not found
     4 *-* command "file2" 
       >>>   " file2"
       +++   "RC(127)"
sh: file3: command not found
     5 *-* command "file3" 
       >>>   " file3"
       +++   "RC(127)"

Keywords in Rexx

The free syntax of REXX implies that some symbols are reserved for the language processor's use in certain contexts.

Within particular instructions, some symbols may be reserved to separate the parts of the instruction. These symbols are referred to as keywords. Examples of REXX keywords are the WHILE in a DO instruction, and the THEN (which acts as a clause terminator in this case) following an IF or WHEN clause.

Apart from these cases, only simple symbols that are the first token in a clause and that are not followed by an "=" or ":" are checked to see if they are instruction keywords. You can use the symbols freely elsewhere in clauses without their being taken to be keywords.

Comments in Rexx

Comments are used to document your code. Single line comments are identified by using the /* */ at any position in the line.

An example is as follows −

/* Main program */
/* Call the add function */
add(5,6)

/* Exit the main program */
exit add:

/* Parse the arguments passed to the add function */ parse arg a,b
/* Display the added numeric values */
say a + b

Comments can also be written in between a code line as shown in the following program −

/* Main program */ 
/* Call the add function */ 
add(5,6) 

/* Exit the main program */ 
exit 
add: 
parse    /* Parse the arguments passed to the add function */ 
arg a,b 

/* Display the added numeric values */ 
say a + b

The output of the above program will be −

11

You can also have multiple lines in a comment as shown in the following program −

/* Main program 
The below program is used to add numbers 
Call the add function */ 
add(5,6) 
exit 
add: 
parse arg a,b 
say a + b

The output of the above program will be −

11
Advertisements