In this section, we will explore the built-in functions provided by REXX. These functions are essential tools that can help you perform a wide range of operations more efficiently. Understanding and utilizing these functions will significantly enhance your REXX programming skills.

Overview of Built-in Functions

REXX provides a variety of built-in functions that can be categorized into several groups:

  1. String Functions: Manipulate and analyze strings.
  2. Arithmetic Functions: Perform mathematical operations.
  3. Date and Time Functions: Handle date and time values.
  4. Conversion Functions: Convert data from one type to another.
  5. Miscellaneous Functions: Perform various other operations.

String Functions

String functions are used to manipulate and analyze strings. Here are some commonly used string functions:

LENGTH

The LENGTH function returns the length of a string.

/* Example: LENGTH function */
str = "Hello, World!"
length = LENGTH(str)
SAY "The length of the string is:" length

Explanation:

  • LENGTH(str) returns the number of characters in the string str.

SUBSTR

The SUBSTR function extracts a substring from a string.

/* Example: SUBSTR function */
str = "Hello, World!"
substring = SUBSTR(str, 8, 5)
SAY "The extracted substring is:" substring

Explanation:

  • SUBSTR(str, 8, 5) extracts 5 characters starting from the 8th character of the string str.

POS

The POS function returns the position of a substring within a string.

/* Example: POS function */
str = "Hello, World!"
position = POS("World", str)
SAY "The position of 'World' in the string is:" position

Explanation:

  • POS("World", str) returns the starting position of the substring "World" in the string str.

Arithmetic Functions

Arithmetic functions perform mathematical operations. Here are some commonly used arithmetic functions:

ABS

The ABS function returns the absolute value of a number.

/* Example: ABS function */
num = -42
absolute_value = ABS(num)
SAY "The absolute value is:" absolute_value

Explanation:

  • ABS(num) returns the absolute value of the number num.

MAX

The MAX function returns the maximum value among its arguments.

/* Example: MAX function */
max_value = MAX(10, 20, 30)
SAY "The maximum value is:" max_value

Explanation:

  • MAX(10, 20, 30) returns the largest value among the arguments.

MIN

The MIN function returns the minimum value among its arguments.

/* Example: MIN function */
min_value = MIN(10, 20, 30)
SAY "The minimum value is:" min_value

Explanation:

  • MIN(10, 20, 30) returns the smallest value among the arguments.

Date and Time Functions

Date and time functions handle date and time values. Here are some commonly used date and time functions:

DATE

The DATE function returns the current date.

/* Example: DATE function */
current_date = DATE()
SAY "The current date is:" current_date

Explanation:

  • DATE() returns the current date in the default format.

TIME

The TIME function returns the current time.

/* Example: TIME function */
current_time = TIME()
SAY "The current time is:" current_time

Explanation:

  • TIME() returns the current time in the default format.

Conversion Functions

Conversion functions convert data from one type to another. Here are some commonly used conversion functions:

D2X

The D2X function converts a decimal number to a hexadecimal string.

/* Example: D2X function */
decimal_number = 255
hex_string = D2X(decimal_number)
SAY "The hexadecimal representation is:" hex_string

Explanation:

  • D2X(decimal_number) converts the decimal number decimal_number to its hexadecimal representation.

X2D

The X2D function converts a hexadecimal string to a decimal number.

/* Example: X2D function */
hex_string = "FF"
decimal_number = X2D(hex_string)
SAY "The decimal representation is:" decimal_number

Explanation:

  • X2D(hex_string) converts the hexadecimal string hex_string to its decimal representation.

Miscellaneous Functions

Miscellaneous functions perform various other operations. Here are some commonly used miscellaneous functions:

RANDOM

The RANDOM function generates a random number within a specified range.

/* Example: RANDOM function */
random_number = RANDOM(1, 100)
SAY "The random number is:" random_number

Explanation:

  • RANDOM(1, 100) generates a random number between 1 and 100.

TRACE

The TRACE function controls the tracing of REXX instructions.

/* Example: TRACE function */
TRACE "R"
SAY "This is a trace example."

Explanation:

  • TRACE "R" sets the trace mode to "Results", which displays the results of each instruction.

Practical Exercises

Exercise 1: String Manipulation

Write a REXX program that takes a string input from the user, extracts a substring, and displays its length.

Solution:

/* Exercise 1: String Manipulation */
SAY "Enter a string:"
PULL user_string
substring = SUBSTR(user_string, 2, 5)
length = LENGTH(substring)
SAY "The extracted substring is:" substring
SAY "The length of the substring is:" length

Exercise 2: Arithmetic Operations

Write a REXX program that takes two numbers as input from the user and displays their absolute values, maximum, and minimum.

Solution:

/* Exercise 2: Arithmetic Operations */
SAY "Enter the first number:"
PULL num1
SAY "Enter the second number:"
PULL num2
absolute_value1 = ABS(num1)
absolute_value2 = ABS(num2)
max_value = MAX(num1, num2)
min_value = MIN(num1, num2)
SAY "The absolute value of the first number is:" absolute_value1
SAY "The absolute value of the second number is:" absolute_value2
SAY "The maximum value is:" max_value
SAY "The minimum value is:" min_value

Exercise 3: Date and Time

Write a REXX program that displays the current date and time.

Solution:

/* Exercise 3: Date and Time */
current_date = DATE()
current_time = TIME()
SAY "The current date is:" current_date
SAY "The current time is:" current_time

Summary

In this section, we covered the following key points:

  • REXX provides a variety of built-in functions for string manipulation, arithmetic operations, date and time handling, data conversion, and miscellaneous tasks.
  • Understanding and utilizing these functions can significantly enhance your REXX programming skills.
  • Practical exercises help reinforce the concepts learned and provide hands-on experience with built-in functions.

In the next section, we will delve into error handling in REXX, which is crucial for writing robust and reliable programs.

© Copyright 2024. All rights reserved