Understanding variables

This is an introduction to variables.

A variable is a program element that can hold volatile data. The following list summarizes variables usage:

  • Variables are declared with the DEFINE instruction or with the VAR instruction.
  • With the DEFINE instruction, the scope of a variable can be global, local to a module or local to a function.
  • With the VAR instruction, the scope of the variable is the code block where the instruction is used, inside a function.
  • When defined at the module level, a variable can be declare as PRIVATE or PUBLIC.
  • Variables can be defined as records, arrays, dictionaries, etc.
  • Variables containing database records are typicaly defined from a database schema with the LIKE clause.
  • Variable can be defined from user-types.
  • After a definition without initializer, variables get default values specific to their type.
  • Default values (or NULL) can be assigned with the INITIALIZE instruction.
  • In the definition of a variable, you can specify a value as initializer, for simple and structured types.
  • In the code, variable assignment is done with the LET instruction.
  • Database validation rules can be applied with the VALIDATE instruction.
  • Variables can be used as SQL parameters or fetch buffers in SQL statements.
  • Interactive instructions (dialogs) use program variables as model to hold the data.
  • Variables can be defined with attributes by using the ATTRIBUTES() clause.