What are variables? |
Topics | |
1. Generalities
2. Variable names
3. Types of variables
4. Arrays
5. Scope
1. Generalities
Variables in Aptilis, like in any other languages,
are 'boxes' where values can be placed. One of the main
advantages is to save some typing. It is easier to
use 'pi' than having to type '3.14159265'
all the time.
This is how you fill a variable with a value:
var = 16
And you can use the variable for its value at any time:
print(16)is equivalent to:
print(var)
You don't have to declare variables, Aptilis reserves some
space for them as they're created, i.e. when you first
mention them.
2. Variable names
Variable names can be made up of letters and numbers
but have to start with a letter. Names can be of any length. (but
keep to a manageable wingspan...)
Meaningful variable names help a lot when it comes to maintenance.
Valid names: a, b, ab, a1, rate, rate5, etc...
Incorrect names: 1,
1a, tax&profit, etc...
What applies to variable names also applies to sub names.
Variable names are case sensitive. 'A' is not
the same as 'a'.
3. Types of variables
In aptilis, there is not really a problem
of variable types. All variable are stored as
text, and their numeric value is calculated when
necessary. Sometimes it has to be indicated
in which context you want an instruction to
handle a variable. By default, the context is
numeric. If you want to work in a string context,
you have to use the dollar sign.
Example: a = "Hello" $
If you had forgotten the dollar sign, Aptilis
would have tried to evaluate the string "Hello" as a number and
would have filled 'a' with the value '0.0000'.
a = "hello" $
print(a) produces '0.0000'
print(a$) produces 'hello'
In fact, a$ and a are indeed the same variable unlike
in some other close languages. Note that the dollar
sign is added at the end of any expression as in:
print(left("hello", 2)$)
to get 'he' and not '0.0000'.
4. Arrays
In aptilis, all variables are arrays, even if you
don't realize it!
For example to create an array with the first 10 multiples
of 3 you can do:
for i=1 to 10 m3[i] = i * 3 end for
Note that 'var[0]' is another way to write 'var'. That is
you need not use the notation [0] to access the first element of
an array.
You do not need to declare arrays prior to using them. Arrays grow as
needed when you use them.
Arrays are used to retrieve values of 'MULTIPLE' select boxes in HTML.
If you have such a box like in:
<SELECT name=choice MULTIPLE> <OPTION>Pizza <OPTION>Lasagne <OPTION>Pasta <OPTION>Chocolate Ice Cream </SELECT>
The options selected by the user are returned in the choice[] array. You can check how many there are with the line:
n = getArraySize(choice[])
String values of array elements are assigned and accessed in the following fashion:
a[2] = "Greetings!" $ print(a[2]$)
5. Scope
Variables in Aptilis may be local or global. But as nothing can happen
outside of a sub, even global variables need to be assigned from within
a sub.
Being global means that you retain your value across subs. A global
variable given the value '24' will retain that value in all subs. Local
variable cease to exist outside the sub they appear in.
A corollary to that is that you can have a variable with the same name
in different subs without interference, but a global variable such as
'_a' is the same for every one.
Global variables need an underscore before their name.
_taxPercent is global
revenue is local.