8.3.2   Scope of Variables (local or global)

HDevelop supports local and global variables. All variables are local by default, i.e., they exist only within their procedure. Therefore, local variables with the same name may exist in different procedures without interfering with each other. In contrast, global variables may be accessed in the entire program. They have to be declared explicitly using the operator global.

The declaration

  global tuple File

declares a global control variable named File, whereas

  global object Image

declares a global iconic variable Image.

The keyword def allows you to mark one declaration explicitly as the place where the variable is defined, e.g., global def object Image. This is only relevant when exporting the program to a programming language. See the description of the operator global for more information.

Once the global variable is declared, it can be used just like a local variable inside the procedure it has been declared in. If you want to access a global variable in a different procedure, you have to announce this by using the same global ... call (otherwise, a local variable will be created).

main procedure:
  * declare global variables
  global tuple File
  global object Image
  ...
  File := 'particle'
  read_image(Image, File)
  process_image()
  * Image has been changed by process_image()
  * File remains unchanged
  ...

process_image procedure:
  * use global variable
  global object Image
  ...
  bin_threshold(Image, Region)
  File := 'fuse'
  read_image(Image, File)
  return()

Because procedures have to explicitly announce their use of global variables, existing procedures cannot be broken by introducing global variables in other parts of the program. By nature, the names of global variables have to be unique in the entire HDevelop program, i.e., all loaded external procedures, the main procedure and all local procedures. The variable window provides a special tab to list all global variables that are currently declared.

Figure 8.2: Global variables.