Variables (and constants) in ugBASIC



Variables Constants Arrays Scope

Variables

ugBASIC is a dynamic programming language (as any standard BASIC language), so you can use variables without defining them before the use using the . Obviously, you can also define them before they're used, and this is called . Moreover, there is a way to require that any variable is explictly defined before use, so that is called .

Automatic variable definition

If an undefined variable is used into the source, ugBASIC compiler will try to define it silently, deducing the data type based on the type of the expression / constant used to initialize the variable. If the variable is initialized using a (integer) numerical constants, the approach used will be the one active at the moment (click here for informations). On the other hand, if the variable is initialized using a floating point constant, another variable or an expression, its type will be used to initialize the variable data type.

For example:

a = 42
PRINT a


The above example will be compiled because ugBASIC defines the variable a in the first line. This means that ugBASIC can work also if no variable is defined. I.e.:

a = 5
a = a + 1000
PRINT a, b


Like other BASICs, variables are automatically initialized to 0. This means that you can assume that the initial value of a variable will be always zero (0). until you have assigned some value to it.


Manual variable definition

The VAR or DIM statement may be used to explicitly define variables. Here are some examples of variable definition using VAR/DIM:

VAR lines AS INTEGER
DIM scoring AS WORD
VAR why AS LONG
DIM surname AS STRING


Apart from the VAR/DIM statement, there are other cases where you may explicitly define a variable. Assigning values to variables by using a suffix symbol (the @, %, &, $ suffixes) is supported in ugBASIC:

a@=-120
a%=42
a&=239485838

When a variable is defined with a type, it can be accessed only with the correct suffix.


Explicit variable definition

In ugBASIC there is a way to force that variables are defined before using them. It is the OPTION EXPLICIT statement. This command can be used ad any point of the program, to force to declare all the variables before using them. When you add this statement alone, ugBASIC will emit a compile error and it highlight the variable in the code that need to be declared. Anyway, it can be also disabled using the OFF parameter:

OPTION EXPLICIT ON
PRINT a : REM this is an error!
OPTION EXPLICIT Official
PRINT a : REM this is *not* an error!

Constants

Constants are literals that will be translated into numeric / string values at compile time. When the compiler encounters a constant, it will take the real value represented. Constant are always "global", i.e. are visibile everywhere. There are many benefits in using contants:
Asserting using constants

An “assertion” is a Boolean expression that must be “true” if and only if the code is working correctly. If the assertion is false, an error is reported the code needs to be corrected. Assertions allow you to insert a "sanity check" mechanism. These controls are used in the testing and development phase.

The ugBASIC language allows to define constant that must be positive (POSITIVE) or in a specific range of values([,],(,],[,),(,)). If the constants are out of ranges or negative, the compilation will stop.


Arrays

Arrays is a vector or a matrix of values, and they are similar to the arrays of other basics. They must be explicitly defined using the DIM. The maximum number of dimensions is 256. A few examples of defining arrays:

DIM AS BYTE vettore(52)
DIM numbers(5)
DIM ipermatrix(5, 5, 5)


Accessing array members is done using the standard BASIC syntax, taking care that array indices starts from zero (0), so the first element is 0, the second is 1 and so on. Note that no boundary check is done at runtime. So, if you use a set of indexes that are out of the limits of the array, the program probably crashes.

Click here for more information about arrays.

Variables scope

Variables can be defined everywhere, but you must take care of the fact that the position of variable definition defines the “scope” of the variable or its visibility. In ugBASIC we have only two scopes:

  • global scope (GLOBAL or SHARED keywords), so the variable is visible everywhere in the program, from main source to every PROCEDURE;
  • local scope, where the variable is visible only inside the PROCEDURE or inside the main program, as well.


If you define a variable outside a PROCEDURE, it will be defined inside the main program (in its “local” scope), which means that it will be not accessible by PROCEDURE. To make it accessible, you need to use the GLOBAL keyword in main source or SHARED keyword inside the PROCEDURE. If you define a variable inside a PROCEDURE, it will be defined in the local scope of that PROCEDURE and it won't be accessible from anywhere else.

Any problem?

If you have found a problem, if you think there is a bug or, more simply, you would like something to be improved, write a topic on the official forum, or open an issue on GitHub.

Thank you!