What are functions in Rexx?

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.

String manipulation

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
Returning the length of myString
  • SUBSTR: This function extracts a substring from a string.

/* Using SUBSTR function */
substring = SUBSTR(myString, 1, 5)
say "Substring:", substring
Returning a substring from myString
  • POS: This function finds the position of a substring within another string.

/* Using POS function */
position = POS("World", myString)
say "Position of 'World':", position
Returning the position of a substring inside myString
  • 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
Manipulating the alignment of myString

Mathematical operations

The following are some basic mathematical operations that are used in Rexx:

/* Mathematical Operations Example */
number = 25
/* Using ABS function */
absoluteValue = ABS(number)
say "Absolute value:", absoluteValue
Taking the absolute of a number
  • 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
Applying the trigonometric functions on angleDegrees
  • SQRT: It calculates the square root of a number.

/* Using SQRT function */
squareRoot = SQRT(number)
say "Square root:", squareRoot
Calculating the square root of a number

Input and output

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 userName
say "Hello, " userName "!"
/* Using PUSH function */
PUSH "This line will be written to the output"
Printing the input and pushing text to the output

Control flow

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 THEN
say num "is greater than 5"
ELSE
say num "is not greater than 5"
Using the if-then-else construct
  • The functions DO and END define a block of code to be executed as shown below:

/* Using DO-END Loop */
DO i = 1 TO 5
say "Iteration:" i
END
Using the Do-End loop
  • 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 <= 5
say "Current counter value:", counter
counter = counter + 1
END
say "WHILE loop finished."
/* UNTIL Loop Example */
counter = 1
/* Using UNTIL loop to print numbers from 1 to 5 */
UNTIL counter > 5
say "Current counter value:", counter
counter = counter + 1
END
say "UNTIL loop finished."
Using the while and until loops

The codes used in this Answer are quite basic and have explanations written in comments for better understanding.

Conclusion

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 documentationhttps://www.ibm.com/docs/en/zos/2.1.0?topic=f-built-in-functions.

Free Resources

HowDev By Educative. Copyright ©2025 Educative, Inc. All rights reserved