Rexx - Variables


Advertisements

In Rexx, all variables are bound with the ‘=’ statement. Variable names are sometimes referred to as symbols. They may be composed of Letters, Digits, and Characters such as ‘. ! ? _’. A variable name you create must not begin with a digit or a period. A simple variable name does not include a period. A variable name that includes a period is called a compound variable and represents an array or table.

The following are the basic types of variables in Rexx which were also explained in the previous chapter −

  • Integers − This is used to represent an integer or a float. An example for this is 10.

  • Big integers − This represents a large integer value.

  • Decimal − A decimal value is a string of numerics that contains a decimal point but no exponent identifier.

  • Float − A float value is a string that represents a number in the scientific notation.

  • String − A series of characters defines a string in Rexx.

Different Types of Variable Functions

In this section, we will discuss regarding the various functions a variable can perform.

Variable Declarations

The general syntax of defining a variable is shown as follows −

var-name = var-value 

where

  • var-name − This is the name of the variable.

  • var-value − This is the value bound to the variable.

The following program is an example of the variable declaration −

Example

/* Main program */ 
X = 40 
Y = 50 
Result = X + Y 
say Result

In the above example, we have 2 variables, one is X which is bound to the value 40 and the next is Y which is bound to the value of 50. Another variable called Result is bound to the addition of X and Y.

The output of the above program will be as follows −

90

Naming Variables

Variable names are sometimes referred to as symbols. They may be composed of Letters, Digits, and Characters such as ‘. ! ? _’ . A variable name you create must not begin with a digit or period.

If a variable has not yet been assigned a value, it is referred to as uninitialized. The value of an uninitialized variable is the name of the variable itself in uppercase letters.

An example of an unassigned variable is as follows −

Example

/* Main program */ 
unassignedvalue 
say unassignedvalue 

If you run the above program you will get the following output −

UNASSIGNEDVALUE
sh: UNASSIGNEDVALUE: command not found
     2 *-* unassignedvalue 
       >>>   "UNASSIGNEDVALUE"
       +++   "RC(127)"

Variables can be assigned values more than once. The below program shows how the value of X can be assigned a value multiple times.

Example

/* Main program */ 
X = 40 
X = 50 
say X 

The output of the above program will be as follows −

50

Printing Variables

The values of variables are printed using the say command. Following is an example of printing a variety number of variables.

Example

/* Main program */ 
X = 40 

/* Display an Integer */ 
say X 
Y = 50.5 

/* Display a Float */ 
say Y 
Z = "hello" 

/* Display a string */ 
say Z 

The output of the above program will be as follows −

40 
50.5 
hello 
Advertisements