DEFINE
A variable contains volatile information of a specific data type.
Syntax
[PUBLIC|PRIVATE]
DEFINE identifier [,...] type-definition [ = initializer ]
[,...]
{ data-type
| LIKE [dbname:]tabname.colname
}
[ attributes-list ]
FUNCTION function-name (
parameter-name data-type [ attributes-list ]
[,...]
)
[ attributes-list ]
[ RETURNS { data-type [ attributes-list ]
| ( [ data-type [ attributes-list ] [,...] ] )
} ]
ATTRIBUTES ( attribute [ = "value" ] [,...] )
{ scalar-initializer
| record-initializer
| array-initializer
}
{ integer-literal
| decimal-literal
| mdy-date-literal
| datetime-literal
| interval-literal
| boolean-literal
| NULL
}
( identifier : initializer [,...] )
[ initializer [,...] ]
- identifier is the name of the variable or record member.
- data-type can be a primitive data type, a record definition, an array definition, a user defined type, a built-in class, an imported package class, or a Java class.
- dbname identifies a specific database schema file.
- tabname.colname references a column defined in the database schema file.
- attribute is an attribute to extend the variable definition with properties.
- value is the value for the variable attribute, it is optional for boolean attributes.
- mdy-date-literal is an
MDY(mm,dd,yyyy)
specification.
Usage
A variable is a named location in memory that can store a single value, or an ordered set of values. Variables can be global to the program, module-specific, or local to a function.
Any program variable needs to be declared with a DEFINE
statement before it
is used.
A variable is typically defined with a data type.
The variable name must follow the convention of identifiers.
By default, module-specific variables are private; They cannot be used by an other module of
the program. In order to improve code re-usability by data encapsulation, we recommend you keep
module variables private, except if you want to share large data (like arrays) between modules. To
make a module variable public, add the PUBLIC
keyword before
DEFINE
. When a module variable is declared as public, it can be referenced in
another module by using the IMPORT FGL
instruction.
When defining variables with the LIKE
clause, the data types are taken from the
database schema file at compile time. Make sure that the schema file of the database schema during
development corresponds to the database schema of the production database; otherwise the variables
defined in the compiled version of your modules will not match the table structures of the
production database. For more details, see Database column types.
To write well-structured programs, avoid global variables. If you need persistent data storage
during a program's execution, use variables local to the module and give access to them with
functions, or make the module variables PUBLIC
to other modules. For more details,
see Declaration context.
Variables can be defined with the ATTRIBUTES()
clause, to specify
meta-data information for the variable. For more details, see Definition attributes.
Variables can be defined to hold a function reference. Best practice is to declare a user-defined type with the function type, then define the variables using the user type.
By adding an equal sign followed by a value, you can save an additional LET
instruction to initialize variables.
Initializers are especially useful to set values of structured variables (records, arrays) . For
more details, see Variable default values.