Rexx (Restructured Extended Executor) provides various built-in functions that we can use in our scripts to manipulate strings, perform mathematical operations, handle dates and times, interact with the environment, and more. In Rexx, functions are predefined routines or commands that perform specific operations.
Let’s take a look at the different types of functions used in Rexx.
The functions used for string manipulation in Rexx are as follows:
LENGTH
: This returns the length of a string, as shown in the example below:
/* String Manipulation Example */myString = "Hello, World!"/* Using LENGTH function */lengthOfStr = LENGTH(myString)say "Length of the string:", lengthOfStr
SUBSTR
: This function extracts a substring from a string.
/* Using SUBSTR function */substring = SUBSTR(myString, 1, 5)say "Substring:", substring
POS
: This function finds the position of a substring within another string.
/* Using POS function */position = POS("World", myString)say "Position of 'World':", position
LEFT
, RIGHT
, CENTER
: These functions manipulate the alignment of a string by left-aligning, right-aligning or centre-aligning it.
/* String Alignment Example *//* Original string */originalString = "REXX"/* Using LEFT function to left-align the string */leftAlignedString = LEFT(originalString, 10)say "Left-aligned string:", leftAlignedString/* Using RIGHT function to right-align the string */rightAlignedString = RIGHT(originalString, 10)say "Right-aligned string:", rightAlignedString/* Using CENTER function to center-align the string */centerAlignedString = CENTER(originalString, 10)say "Center-aligned string:", centerAlignedString
The following are some basic mathematical operations that are used in Rexx:
ABS
: It returns the absolute value of a number as done below:
/* Mathematical Operations Example */number = 25/* Using ABS function */absoluteValue = ABS(number)say "Absolute value:", absoluteValue
SIN
, COS
, TAN
: These represent the typical trigonometric functions, namely sine, cosine and tangent functions.
/* Trigonometric Functions Example *//* Angle in degrees */angleDegrees = 45/* Convert degrees to radians */angleRadians = angleDegrees * (3.141592653589793 / 180)/* Using SIN function */sinValue = SIN(angleRadians)say "Sine of", angleDegrees, "degrees is:", sinValue/* Using COS function */cosValue = COS(angleRadians)say "Cosine of", angleDegrees, "degrees is:", cosValue/* Using TAN function */tanValue = TAN(angleRadians)say "Tangent of", angleDegrees, "degrees is:", tanValue
SQRT
: It calculates the square root of a number.
/* Using SQRT function */squareRoot = SQRT(number)say "Square root:", squareRoot
Let’s look at the input and output functions used in Rexx:
PULL
: It reads a line from the standard input.
PUSH
: It writes a line to the standard output.
The code that uses both functions is shown below where the input is taken from the user using the PULL
function and printed via the PUSH
function:
/* Input/Output Example */say "Enter your name:"PULL userNamesay "Hello, " userName "!"/* Using PUSH function */PUSH "This line will be written to the output"
The final most important set of functions involve control flow in which:
Conditional statements like IF
, THEN
, ELSE
are used.
/* Control Flow Example */num = 10/* Using IF-THEN-ELSE */IF num > 5 THENsay num "is greater than 5"ELSEsay num "is not greater than 5"
The functions DO
and END
define a block of code to be executed as shown below:
/* Using DO-END Loop */DO i = 1 TO 5say "Iteration:" iEND
Looping constructs, namely WHILE
and UNTIL
, are used to iterate over blocks of code multiple times.
/* WHILE Loop Example */counter = 1/* Using WHILE loop to print numbers from 1 to 5 */WHILE counter <= 5say "Current counter value:", countercounter = counter + 1ENDsay "WHILE loop finished."/* UNTIL Loop Example */counter = 1/* Using UNTIL loop to print numbers from 1 to 5 */UNTIL counter > 5say "Current counter value:", countercounter = counter + 1ENDsay "UNTIL loop finished."
The codes used in this Answer are quite basic and have explanations written in comments for better understanding.
In conclusion, Rexx functions play a pivotal role in enhancing the capabilities of Rexx scripting by providing a diverse set of tools for string manipulation, mathematical operations, input/output operations, and control flow. In this Answer, we covered the most fundamental functions used in Rexx. To learn more about various functions used in Rexx, check out this
Free Resources