Variables are simply names used in a program, where the names represent stored numeric or string values. When an expression contains a variable, such as the expression (x+2), PowerBASIC replaces the variable with its value and computes the value of the expression, as demonstrated in the following example.
x = 2 'x is a variable that is given the value of 2 y = x + 2 'y is a variable whose value is now 2 + 2 = 4
AS the example shows, variable values can be changed at any time during the program.
Declaration of a variable before using it is not required, although a compiler directive #DIM ALL can be added to a program's source code to have the compiler require variable declaration prior to use. It is considered good programming practice to require variable declaration.
Here's an example of a simple variable declaration, using the DIM statement.
#DIM ALL 'forces use of DIM to define variables before use DIM x as Single 'variable x defined as single precision number DIM j As Long 'variable j defines as long integer x = 2.3 'without DIM statements, this line causes an error j = 5 'without DIM statements, this line causes an error
The other declaration statements, REDIM, LOCAL, GLOBAL, STATIC, and THREADED, are discussed in the section on variable scope.
In PowerBASIC all variable names begin with a letter. Variable names can be up to 255 letter and digits,
Note that long variable names do not affect memory storage requirements. So feel free to use descriptive names of any length to make it easy for you to read and maintain your code. Other that how much typing is involved, there is no penalty for using long variable names.
PowerBASIC is not case-sensitive. The variables dog$ and DOG$ are the same variables.
However, note that x$, x%, and x& are different variables and can coexist in a program. This is not recommended because of the visual confusion it can create.
Creating a Variable
Unlike some languages which require that you define a variable before using it, PowerBASIC allows you to define a variable by simply using it in a line of code.
a$ = 5 # b = 2.5 #
When a variable has not yet been assigned a value, PowerBASIC sets the value to undef, which is similar to NULL in other languages. The PowerBASIC defined function can test to see if a variable has been assigned a value.
Good Variable Naming Practice
Using variable names which describe the content of the variable is considered good practice. For example, size$ is preferred over $s.
Programmers also use the following two common variable naming practices, both of which involve using more than one word in the variable name.
last_index # words separated by an underscore LastIndex # capitalizing words in the variable name
It is also considered good practice to use Type Specifier suffixes on variable names because it gives a visual indication of the type of data that the variable expects. This is particularly useful during program debugging. Here are the two examples from above, with integer type specifiers included.
last_index% # words separated by an underscore LastIndex% # capitalizing words in the variable name
Variable Naming Exceptions - Special Characters
There are two exceptions to the naming rules - type specifiers and contstants. Type specifiers allow other characters as suffixes to a variable name, whereas constants use variable names with other characters as prefixes to the variable names.
DIM x As Long DIM x% 'type specifier % used to declare x as integer DIM x$ 'type specifier $ used to declare x as integer
PowerBASIC allows both numeric and string equates. The name of a numeric equate always begins with a % character. The name of a string equate always begins with a $ characters. The following examples show how equates are defined.
%MyEquate = 5 'MyEquate defined as numeric $MyEquate = "hello" 'MyEquate defines as string
Equates has global scope and must be defined outside a procedure (Sub/Function), normally at the start of an application's source code. Equates can be defined using other equates, provided the other equates have already been defined. Also, equates must be defined using only simple expressions - those consisting of numeric equates, bitwise operators (such as AND, OR, NOT), arithmetic operators +, -, *, /, and \, and the relational operators >, <, >=, <=, <>, =, and the CVQ function.
Unlike variables, which are given default values when declared, equates must be assigned before it is referenced, even if that value is zero. Failure to set the value of an equate before use will cause an error during compilation.
If you have suggestions or corrections, please let me know.