|
Variable declaration
To use a variable, you need to declare it. A
variable declaration consists in giving its name,
its type and its local or public character.
A public variable can be used anywhere, in any
function or procedure, but a local one will be
only available in the function or procedure it
is declared in. This is why you can't declare
a local variable outside of a function or a procedure.
Public variable declaration
You should better put public variable declarations
outside of a function or a procedure.
Public VariableName as TypeVariable
|
Example :
Public NumberOfPlayers as Integer
|
A public variable, once declared, can be used anywhere in the program. In ETP-Basic, public variables are static : that means that the values are kept even if the user quits the programs and re-runs it.
Local variable declaration
Local VariableName as VariableType
|
Example:
A local variable can only be used in the body of the function or procedure it was declared in. In fact, when the execution of the function or procedure is finished, local variables are deleted. This is a good way to program recursions.
You should only use local variables when they are only used in the function or procedure you declare them in. If you're a beginner in the programmation universe, you should learn that it isn't good to have public variable if not needed.
A local variable must be declared in the body of the function or procedure where it will be used. Elsewhere, it will be a nonsense !
You can declare a public variable in the body of a function or a procedure, but because it can be used by other functions (and so,it may be modified), the compiler will return a WARNING message.
Implicit
Declaration (If you forget to declare...)
If you forget to declare a variable, it is not a
problem. The compiler will declare it according
to the use you make of this variable. This is called
implicit declaration. For example, if you
try to affect a variable you haven't declared, the
compiler will declare it for you. You should better
declare it and explicitely give its type to be sure
of the result during execution and to have a clean
code.
For some expressions, more than one type can be used. So you must be careful for the type chosen for the variable. For example, when you write :
Integer and Long types are both correct for the variable a.
If you loke to use another type as the one chosen by the compiler, you will have to do it by yourself.
|