VAR
The VAR
instruction declares a program variable within a code
block.
Syntax
VAR
{
identifier [
,...]
type-specification
|
identifier [
primitive-type-specification ]
= primitive-initializer
|
identifier user-type-specification [
= user-type-initializer ]
|
identifier record-type-specification [
= record-initializer ]
|
identifier array-type-specification [
= array-initializer ]
|
identifier dictionary-type-specification [
= dictionary-initializer ]
|
identifier function-type-specification [
= function-reference ]
}
[
,...]
where type-specification is:
{
primitive-type-specification
|
user-type-specification
|
record-type-specification
|
array-type-specification
|
dictionary-type-specification
|
function-type-specification
|
other-type-specification
}
- identifier is the name of the variable, that must follow the convention for identifiers.
- primitive-type-specification is a primitive type definition. The primitive type can be omitted, if a primitive-initializer is used.
- primitive-initializer is an expression or a simple scalar initializer. When using this initializer without a preceeding type specification, the compiler will automatically deduce the variable type.
- user-type-specification is the name of a user-defined type, followed by an optional variable initializer, corresponding to the user-type-specification.
- record-type-specification is a record definition, followed by an optional record-initializer.
- array-type-specification is an array definition, followed by an optional array-initializer.
- dictionary-type-specification is a dictionary definition, followed by an optional dictionary-initializer.
- function-type-specification is a function definition, followed by an optional function-reference.
- other-type-specification is one of:
- The name of a built-in class
- The name of an imported extension class
- The name of an imported Java class
Usage
The VAR
instruction is similar to the DEFINE
instruction, in
the sense that it declares a program variable, inside the body of a FUNCTION
.
While the DEFINE
instruction can only be used at the top of the function body,
the VAR
instruction can declare new variables specifically for the code block where
it is used.
The typical use of the
VAR
instruction is when you have to define temporary
variables for a piece of code, only needed in that code block. With the DEFINE
instruction, in large functions, that can end up with many variable definitions at the top of the
function body, with the constraint of unique variable
names:FUNCTION process() RETURNS ()
DEFINE tmp1 INTEGER
DEFINE tmp2 DECIMAL(10,2)
DEFINE tmp3 STRING
IF TRUE THEN
LET tmp1 = 555
DISPLAY tmp1
END IF
IF TRUE THEN
LET tmp2 = 200.45
DISPLAY tmp2
END IF
IF TRUE THEN
LET tmp3 = "hello"
DISPLAY tmp3
END IF
END FUNCTION
With
the VAR
instruction, variables can be declared individually in each code block,
using the same name, but different types:FUNCTION process() RETURNS ()
IF TRUE THEN
VAR tmp = 555
DISPLAY tmp
END IF
IF TRUE THEN
VAR tmp DECIMAL(10,2) = 200.45
DISPLAY tmp
END IF
IF TRUE THEN
VAR tmp = "hello"
DISPLAY tmp
END IF
END FUNCTION
While the VAR
instruction increased code readability of complex functions, it is
not recommended to write large functions. When possible, consider splitting code into individual
functions, to increase reusability and readability.
Inside a given function body, variable names declared with the
VAR
instruction
cannot repeat in nested code blocks. This rule keeps the code readable. For example, the following
code is
invalid:FUNCTION getCount() RETURNS INTEGER
VAR cnt = 0
FOR cnt=1 TO 10
DISPLAY cnt
END FOR
IF TRUE THEN
VAR cnt = 0
| The symbol 'cnt' has been defined more than once.
| See error number -4319.
SELECT COUNT(*) INTO cnt FROM customer
END IF
RETURN cnt
END FUNCTION
The
VAR
instruction allows to declare and initialize the variable from an
expression. The variable gets implicitly the type of the
expression:FUNCTION getCount() RETURNS INTEGER
...
VAR cnt = getCount() -- Defined as INTEGER
FUNCTION getCustName() RETURNS VARCHAR(50)
...
VAR name = getCustName() -- Defined as VARCHAR(50)
The expression used in a
VAR
instruction can raise an exception, that can be
trapped:MAIN
TRY
VAR myvar = 1 / 0
DISPLAY myvar
CATCH
DISPLAY "Error: ", status
END TRY
END MAIN
If a variable declared with the
VAR
instruction is not used in its context, the
fglcomp compile will produce the error -6615:MAIN
VAR v1 INT = 1000
DISPLAY v1
VAR v2 INT = 2000
| The symbol 'v2' is unused.
| See error number -6615.
END MAIN