Variables and Constants

You can create variables in your OmniMark program. Variables may be of any supported data type, including record data types defined using declare record.

Variables can be either global or local. The difference between these is that global variables exist (and can be used) everywhere within a program, and local variables only exist (and can be used) within the rule or function where they are declared.

Since variables cannot be used until they are declared, global variable declarations usually appear at the top of an OmniMark program, and local variables appear at the beginning of the rule or function in which they are to be used. The "scope" of a variable (global or local) must be indicated in the variable declaration.

A variable declaration that creates a global integer variable named "count1" looks like this:

  global integer count1

Once declared, the variable "count1" can be used to store any positive or negative integer value.

To create a local stream variable named "quotation", you would use the variable declaration:

  local stream quotation

To store a string in a stream variable, you can use the set keyword. For example:

  set quotation to "Is this a dagger which I see before me?"

Integer variable values can be set and changed the same way as stream variables using the set action, but integer variables can also be manipulated using the increment and decrement actions. For example, to increase the value of the count1 variable by 1, you need only say:

  increment count1

It is possible to increment or decrement the value of an integer variable by the value of another integer variable. For example, you could decrement the value of "count1" by the value of "count2" with the following code:

  decrement count1 by count2

The following is a program that makes use of a global switch variable to decide which output action should be executed:

  global switch question
  
  process
     set question to true
  
  process
     do when question  ;checks if question is true
        output "to be"
     else
        output "not to be"
     done

Note that the output of this program will always be "to be".

Initial values

It is possible to declare a variable with an initial value:

  global integer count2 initial {3}
  global stream quotation2 initial {"A horse!"}
  global switch status2 initial {true}

You can set a variable to the value of another variable. For example, the process rule in the following program will set the value of the global integer variable "var1" to the value of the local integer variable "var2" and give you the output "8":

  global integer var1
  
  process
     local integer var2
     set var2 to 8
     set var1 to var2
  
  process
     output "%d(var1)"

Constants

A constant is like a global variable, except that it must be given an initial value, and its initial value cannot be changed. A constant is declared by using the keyword constant instead of global. A constant is a good way to give a name to a value that the program will never change.

  constant integer days-in-week initial {7}
  constant integer months-in-year initial {12}