OML for the complete beginner, Lesson 2

Variables

(Note that spacing in equations is generally optional, but recommended. It's generally considered proper programming style to include spaces before and after every symbol, so that it's easier for others to read. In other words, 2+3=5 may be the same as 2 + 3 = 5, but it is far easier to follow what's going on in the second example. This is a stylistic thing, and as such is up to the individual programmer to decide what works best.)

You probably remember from high school algebra that there are constants (that is, numbers: 4 is 4, has always been 4 and will always be 4), and variables (that is, the ever-present x and y, that end up being whatever they end up being and can change from problem to problem).

Variables in macros work much like you remember from mathematics classes; they take the place of an unknown value (or in this case, a value subject to change)

x = 1
y = x + 3
z = x + y

The preceding cases take the form of definitions. I could tell you that "frabjous" means "excellent", and you would probably believe it until you were later told that "frabjous" means "joyful". Your definition of "frabjous" changed. This is the same as telling the computer that x = 1 and then later saying that x = 2. The definition of x can vary. Thus, it is "variable".

Since we're dealing with definitions, there should only be a single term on the left-hand side of the equal sign. Otherwise, the computer can get confused (and so can the programmer).

As the above example shows, it's also possible to use variables to change the meaning of a variable. In this case, x = 1, y = 4 and z = 5. It's also possible to change the meaning of a variable by using itself.

x = 1
x = x + 1

In the first line, you tell the computer what x starts at--in this case, 1. In the second line, you tell it to take the old value of x, add 1, and make that the new value of x. Thus, when those two lines are done executing, x equals 2. If you hadn't already defined x, then x = x + 1 is still valid; x starts out as "nothing," so x + 1 is 1.

OML doesn't care much what you use for variable names, so you can use almost any combination of letters, numbers, and words or upper or lower case, as long as you don't duplicate a command word--that would confuse the computer. The following lines are all valid statements:

x = 1
pd = 56
newpage = 3
newpage2 = 4
SRT = SRT + 3
SomeLongVariableName = SomeOtherLongVariableName

There are several types of variables (a.k.a. "data types"), and not all of them refer to numbers! Here is a list of those you will probably see the most:

Integer Any whole number between roughly -32,000 and 32,000
String Any words or text, including numbers and symbols; everything is treated as "words" instead of actual numbers or symbols
Long Any number between roughly -2 billion and 2 billion
Variant Any of the above, chosen at time of first use

Next time, more details on how these act and interact...


Return to Lesson #1.
Return to Main page.

Copyright 2003, Joel A. Hahn
Sponsored and endorsed by OCLC Online Computer Library Center, Inc.