Engineering Tip
Most people can write math and logic code that works, but not everyone write it such that it runs efficiently. Since IML can be the most critical task controlling your process, your goal should be to not only write code that is functional, but also to write code that runs efficiently.
Think of this as a primer for using variables in IML. Contained on this page are some examples, which will provide some guidelines for developing well-written, efficient IML code.
![]()
Generally, IML procedures look like:
proc procname
begin
#code/logic goes here
end
The first procedure in the file must also be the name of the file. The procedure name is case sensitive when compared to the procedure name used in the Math and Logic Triggers table.
![]()
Most IML code is lacking local variables. These non-tag variables are defined in the procedure file and are more efficient to use than tags (tags get their values from the kernel and there is overhead in every tag read/write operation.)
Variables declared before any procedures are global or public to all procedures in this file. Their value is retained for the duration of the running IML task. NOTE: public variables are not available to procedures in other files.
As a naming convention, you should adopt a standard providing that all local (non-tag) variables start with an underscore character. This makes it easier to look at code and differentiate between variables and tags. You should also adopt the convention of using ALL CAPS for constants, capitalized words for public variables and lower-case for private variables.
const _PI 3.1415926536
declare short _Publicvariable
proc example
begin
declare float _privatevariable
# code/logic goes here
end
Variables defined within the procedure are private to this procedure and lose their value between executions of this procedure. Note the use of the underscore and all lower case characters for private variables.
![]()
Q: Why use local variables?
A: They are faster than tags.
Q: Why use tags in IML
A: Another task or other procedure file needs or provides this value
A rule of thumb is to copy the contents of a tag to a variable if you use the tag 3 or more times.
proc example
begin
declare short _tagname
declare short _othertag
_tagname = tagname
_othertag = othertag
if _tagname = 0 then
_othertag = _othertag + (_tagname * 1024)
endif
if _tagname = 8 then
_othertag = _othertag + (_tagname * 2048)
endif
if _tagname = 16 then
_othertag = _othertag + (_tagname * 4096)
endif
othertag = _othertag
end
By using the tag name as the name of the variable with a leading underscore, the value is more apparant when debugging. Because tags cannot begin with an underscore, there will never be a collision.