You are here: Home > Knowledge Refreshers

KR-216* REXX Day 5 (operators and obtaining user input)

 

We have all the regular mathematical operators. Some others (which are represented differently in each programming language) are:

Operator Meaning
\= not equal
& And
| Or
// Remainder function
<> not equal to
|| String concatenation

 

String concatenation can also be done without specifying the pipes as well.

 

Obtaining user inputs

  • The PULL command is used to get input from the user (like scanf and cin).
        PULL variable-name
    will pull the value for variable-name from the terminal when the user inputs the value when prompted.
  • ARG is like command line arguments in C++. We can pass values to the program when we invoke the program for execution. To get these values we use:
        ARG variable-name1 variable-name2 …
  • So PULL is to get values during execution while ARG is to get the values passed to the program when execution begins.

 

KR-217* DAY 6 of REXX (strictly equal and arrays)

 

Before continuing we need to take a look at a special comparison operator that REXX provides (which many other languages do not): ¤ The operator = = means check if strictly equal while \= = is used for strictly not equal check. ¤ Strictly equal checks for equality of spaces and the casing of the value where normal equal (single =) does not check for these.

Code Output
/* REXX */
IF "JABBRADAMUS" == " JABBRADAMUS " THEN
  SAY 'STRICTLY EQUAL'
IF "JABBRADAMUS" = " JABBRADAMUS " THEN
  SAY 'JUST EQUAL'
EXIT
JUST EQUAL

Let’s take a look at one of the most important variable types in any programming language.

Arrays

  • In REXX arrays are called compound variables or stem variables.
    STATE.1
    STATE.2
    STATE.3
  • Usual convention is to store the total number of elements in the array in the 0th element.
    STATE.0 = 3
  • Example code snippet

    Code Output
    /* REXX */
    STATE.0 = 3
    STATE.1 = 'CN'
    STATE.2 = 'AN'
    STATE.3 = 'BN'
    DO I = 1 TO STATE.0
      SAY STATE.I
    END I
    EXIT
    CN
    AN
    BN

To uninitialize the entire array we can use the statement:

    DROP STATE.

(there is a period after STATE to denote that this is an array).

 



KR-218* DAY 7 of REXX (functions and subroutines)
  •  Both are small routines written within the same module (internal) or outside (external).
  • The difference is that functions should return a value (while subroutines need not; if it does then it is stored in the special variable RESULT) and functions are called directly by name but subroutines are called using the CALL statement.
  • Sub routines end with the RETURN statement to return control back to the caller. Functions end with RETURN value (where value is passed back to the caller).
Code Output
/* REXX */
A=5
B=10
CALL ADD
SAY 'BACK TO MAIN'

ADD:
 SAY A+B
  RETURN
EXIT
15 
BACK TO MAIN 
15

 

Guess why 15 is displayed again in the above snippet?

  • Functions will also be the same as a subroutine except that we should return a value.
Code Output
/* REXX */
A=5
B=10
C=ADD_FUNC()
SAY 'FUNCTION CALL C:' C
EXIT

ADD_FUNC:
  RETURN A+B
FUNCTION CALL C: 15

 

To make variables within a subroutine/function as local then use the PROCEDURE keyword.

Code Output
ADD_FUNC: PROCEDURE
A=1
B=2
RETURN A+B
Result of this function will be 3




KR-219* DAY 8 of REXX (passing arguments to subroutines)

Answer to yesterday’s question: Why does 15 appear twice?

Code Output
/* REXX */
A=5
B=10
CALL ADD
SAY 'BACK TO MAIN'

ADD:
 SAY A+B
  RETURN
EXIT
15 
BACK TO MAIN 
15

  • After the SAY command the code will just drop into the next statement which happens to be the start of the ADD routine; so again SAY A+B is executed (this is the 2nd time) and then RETURN is encountered. But this time ADD wasn’t called by anyone so RETURN is as good as EXIT and the program terminates.

Passing argument

  • In an internal routine the routine can access all variables within the same code (kind of like everything is globally visible).
  • But this won’t be possible in the case of an external routine – to pass information here the caller should pass arguments and the routine will use the ARG statement to retrieve the arguments passed to it.

Code Output
/* REXX */
B=10
CALL PROPHECY B /*CALLING WITH ARGUMENT*/
EXIT

PROPHECY:PROCEDURE
 ARG YEARS /*OBTAINING THE ARGUMENT*/
 SAY 'JABBARDAMUS COMES IN:' YEARS ' YEARS'
RETURN
JABBARDAMUS COMES IN: 10 YEARS
  • Multiple arguments can be separated by commas (max. of 10 arguments); similar is the case for functions as well – only difference is that here we will pass the arguments within the brackets used in the function call. Ex: SUM = ADD(a,b,c)