Variables

Variables represent numeric or string values.  Unlike constants, the value of a variable can change during program execution.  Like labels, variable names must begin with a letter and can contain up to 255 letters and digits (although in practical terms you really cannot exceed the length of a line).  Be generous in naming important variables.  In PowerBASIC, long variable names do not steal run-time memory.

The Single-precision variables, EndOfMonthTotals and emt, both require exactly four bytes of run-time storage.  A good rule of thumb is to preserve a balance, keeping variable names short enough so that statements can fit on one line.  Many programmers use single-letter variables for loop counters (i, j, k, l and x, y, z are favorites).  However, you can use names like count, total, index, and so on for greater clarity, especially if you have nested loops.

PowerBASIC has many built-in variable types: Dynamic string; Fixed-length string; nul-terminated string; Field, Integer; Long integer; Quad integer; Byte, Word; Double word; Single; Double; and Extended floating point; Currency and CurrencyX; Variant, Object, Guid, plus Pointer, arrays, and Bit and Sbit bitfield subtypes.

Declaring a variable as a specific type:

Use the DIM statement to declare a variable and use the AS type syntax:

DIM iVar AS INTEGER

Appending a type-specifier to the variable name:

bat# = 1.312 ' bat# is a Double-precision variable

hat% = 3     ' hat% is an Integer variable

DEFINT c     ' Variables beginning with c are now Integer

cats = 16    ' cats is an Integer variable by DEFINT

Bear in mind that cat?, cat%, cat&, cat&&, cat!, cat#, cat##, cat@, cat@@, and cat$ are ten separate variables.  Although using cat over and over again to create different variables like this is legal, good programming practice suggests that you use somewhat different names for different variables.  It is also much better to use descriptive and more easily understood names for your variables rather than single letters.  It's extremely difficult to debug a program in which x@ has been entered instead of x! or x#.  Imagine the confusion of trying to distinguish x&& and x&.  If you had used variable names like count!, result#, remain##, and company$, you would have had considerably less trouble keeping your variables (and their types) apart.

 

See Also

Default Variable Typing

Variable Scope