# Data Types, Literals and Variables

Variables |

A variable is a named location in memory that is used for storing data temporarily during program execution. In PxPlus, the methods for creating/changing variables include assigning (using the LET directive) and inputting (using the INPUT or READ directives). The data type is determined when the variable is created, either string or numeric (not both). The initial value of a numeric variable is 0 (zero). The initial value of a string variable is a null string.

The two variable/data types are distinguished by the fact that string variables have names that end with a $ dollar sign. In the following example, the numeric variable X is assigned the number 1234 and string variable X$ is assigned the text "START TEST, X=".

Example:

> LET X=1234,X$="START TEST, X="; PRINT X$,X
> START TEST, X= 1234

The first character of a variable name must be alphabetic (A through Z); the remaining characters may contain any of the following: A through Z, 0 through 9, _ underscore, or . period. By default, variable names are not case-sensitive but are listed in uppercase only. The 'MC' and 'LC' parameters can be used to maintain mixed or lowercase variable names in listings. Variable names cannot start with FN, as this denotes a user-defined function. See DEF FN directive.

Variables should have unique names that do not conflict with PxPlus keywords. It is also best to avoid three-character names - PxPlus reserves three-character names for system variables. See System Variables to access a complete list.

See Reserved Words for a complete list of reserved words.

A % percent sign before a variable name is used to denote a global variable - a percent sign following a variable name indicates that the contents is an integer. Every variable in PxPlus is defined for a specific scope, which indicates to what extent the variable can be accessed and used (local or global). Typically, a variable is only visible to subroutines within the current program that created it.

## Global Variables

The scope of a variable can be extended for "global" use if it is named with a leading % percent sign; e.g. %VAR1 will be visible to all programs that are executed within a given PxPlus session.

#### Note:
Global variables can only exist within one session of PxPlus - they cannot be shared or carried between sessions.

Remember that VAR1 and %VAR1 are two different variables. The variable named VAR1 can be deleted at any time using the CLEAR or BEGIN directives; whereas, the one named %VAR1 would remain active until the end of the user session or the execution of a START directive.

## Local Variables

The scope of a variable can also be narrowed for "local" use if it is declared using the LOCAL directive. This means that an existing variable can be reassigned for the duration of a specific subroutine, subprogram, for/next loop, or user-defined function. The local declaration does not affect the original contents of the variable. If the variable name is already in use, the system preserves the current value. Once the stack entry has been removed, the system restores the variables to their original values.

Example:

In the following example, the variables X$, I, and N are declared LOCAL for the duration of the subroutine:

> 0130 GOSUB 1000
> ....
> 1000 REM Subroutine 1
> 1010 LOCAL X$, I, N
> 1020 READ (1, KEY=K$) X$
> 1030 I = POS(","=X$)
> 1040 IF I <> 0 THEN X$(I,1)=" "; N++; GOTO 1040
> 1050 PRINT "There were ",n, " commas"
> 1060 RETURN

Original values will be restored upon execution of the RETURN directive. The local declaration can also be placed in front of variables within a DEF FN definition.

Example:

> DEF FN%DATE$(LOCAL DT$) = DT$(1,2)+"/"+DT$(3,2)+"/"+DT$(5,2)