A variable is a named "container" that can hold a numerical or string value and which can be referenced or changed by the program (as opposed to CONSTant values which never change).
Variables in QB64 can be any name except the names of QB64 or QBasic keywords and may not contain spaces or non-alphabetical/non-numerical characters (except "." and "_"). Numerical characters cannot be used as the first character of a variable or array name. QB64 reserves the use of a single leading underscore for QB64 procedural or variable type names.
Variable values can be passed to sub-procedures by using the name as a SUB or FUNCTION parameter in a CALL. Variable names in the main program module can be passed to sub-procedures by using DIM SHARED without using them as CALL parameters.
Variable names dimensioned in SUB or FUNCTION procedures can be the same as names used in the main procedure and can hold different values. SHARED sub-procedure variables can only be shared with the main program module!
Dot variable names are normally used with TYPE variable definitions. The first name before the dot is the name of the variable dimensioned AS the type. The name after the dot is the name of the variable assigned inside of the TYPE.
Variables can be defined as a specific type using a variable type suffix or by using a DIM or REDIM(for dynamic arrays only) statement AS a variable type of _BIT, _BYTE, INTEGER, LONG, SINGLE, DOUBLE, _INTEGER64, _FLOAT or STRING in QB64.
STATIC variables are also initialized to empty values, however, STATIC variables will retain their values in between SUB or FUNCTION calls, and exist for the life of the program.
Arrays variable names can hold many values in one variable name by specifying a reference index enclosed in parentheses.
Variables are used to hold program data information that is attained through the program flow, either by user input, calculations or by other ways of communicaton (as with I/O, memory, TCP/IP or files).
Assignment of variable values can be done using the = assignment symbol (variable1.number = 500, for example).
To pass variable values to a sub-procedure without altering or affecting the variable name's value, parenthesis can be used around the variable name in the CALL.
Example of different usages of variables:
max = 1000 DIM d(max) FOR c = 1 TO max d(c) = c + d(c - 1) NEXT PRINT "Show the result of the addition from 1 to n (1+2+3...+n)" PRINT "n = (0-" + LTRIM$(STR$(max)) + "): "; INPUT "", n IF n <= max AND n >= 0 THEN PRINT d(n) ELSE PRINT "Invalid value (only 0 to" + STR$(max) + " is permitted)."
Show the result of the addition from 1 to n (1+2+3...+n) n = (1-1000): 10 55