Declaring Variables
Variables in SIMPOL must be declared before they can be used. They also do not carry any sort of type designator, as is common in various dialects of BASIC, such as a dollar sign for strings or a percent symbol for integers. Currently there is only one method available for declaring variables. A program statement must begin with the type designator and be followed directly thereafter by the variable name.
function main() string s s = 'foobar' end function s
In the preceding example the variable s
is declared to be of type
string
before the text value foobar
is assigned to it.
One of the more interesting things that this syntax allows is to declare a variable to be
of a type that may be unknown to the programmer at the time that the program is written.
This can occur when a database field is passed to a function and a variable must be
declared to be of the same type as the contents of the field. The example that follows
demonstrates this:
function display(ppcstype1record r, ppcstype1field fld) string s fld.datatype temp if temp.type == string s = r.get(fld) else if temp.type == integer s = .tostr(r.get(fld), 10) end if end function s
In this example the variable temp
is declared to be of the same type
as the datatype of the field. Each time the function is called it may be passed a field
with a different content type. Similarly, a function could return a different value each
time it is called, if the return value is dependent on one of the parameters passed and
the return value is declared by using the datatype of one of the parameters that is
passed into the function.
Although these capabilities can provide considerable flexibility and power when designing programs, it is also possible in even a medium sized program to lose track of the type of a variable, especially if that variable is dependent on the datatype of a database field. It is therefore strongly advised that in any larger function that some sort of naming convention be adopted for naming variables. It isn't necessary to make them as complicated as the notation often associated with Windows C programmers. Since there is no limit to the size and precision of integers and numbers in SIMPOL and no significant pointer capability, it usually sufficient to indicate the type with a single letter. one convention that may show up regularly in the supplied examples is to use a lowercase letter to indicate the type, then an uppercase letter and then the remainder is lowercase or in some cases title case where words are joined together. Generally we use: s for string, i for integer, n for number, b for boolean, d for date, t for time, dt for datetime, fsi for fsfileinputstream, fso for fsfileoutputstream, r for record, ppcs for ppcstype1, etc.
There is no concept of the REDIM
keyword in SIMPOL. If a variable is declared
at one place in the function, and then there is a new declaration using the same variable name
at another place in the function (even if the type changes), this is not an error. The variable is
considered to be destroyed at that point and a new variable is created of whatever type
designation has been used in its declaration. This feature can, however, lead to errors that
may be hard to detect, so it is important that the programmer be cautious in their use and reuse
of variables. There is no advantage to the compiled program of using one variable name twice
or two different variable names. From the point of view of program maintenance, it may be
better practice not to use this feature unless it is abundantly clear from the program why it
was used.
Another important point to remember is that variables in SIMPOL do not automatically initialize
to zero or the empty string. The initial value of any variable is .nul
. This may
cause some confusion at the beginning since any operation that includes a value that equals
.nul
is also going to equal .nul
. Always remember
to initialize any variable before using it! Also, if a variable is not initialized before it
is passed to a function, then the local variable in the function will also be equal to
.nul
until a value is assigned to it. More importantly, since there is no object
to which to assign the results when the function returns, nothing can be passed back to the
calling function in that parameter.