User-defined Functions

In addition to the built-in functions, you can create custom calculation functions. After you create a custom calculation function, it is available for use with other calculations as well.

Functions are useful as shortcuts for large blocks of source code. By creating a function out of commonly used calculation formulas, you can save time and effort instead of typing a few lines of calculation formula every time you want to perform the same operation, it is compressed to a single line.

The syntax of a function is simple:
Function functionname (variable list) 
   [calculation formulas]
End Function

The operations a function performs are contained within the Function / End Function statements. If you need to send data to the function a tag name, for example you simply create a variable in the function's parameters to receive the data. Multiple variables must be separated by commas. These variables exist only within the function.

The following is an example of a function. This function, named checkValue(), looks at a tag and assigns it an alarm if it is over a specified value.

A Function to Assign an Alarm to a Tag Based on a Condition

The following function, named checkValue, assigns an alarm to a tag if the tag value reaches a specified value.
Function checkValue (tagname,sourcename,value) 
  If CurrentValue(tagname) > value Then
  Set AlarmObj = new Alarm 
  AlarmObj.SubConditionName = "HI" 
  AlarmObj.Severity = 750
  AlarmObj.NewAlarm
  "alarmname", "Simulated", "tagname", "Now" 
  checkValue = true
Else 
  checkValue = false
  End If
End Function
If you want to use this function, enter the values for tag name, source name, and value, as shown in the following example:
alm_set = checkValue("DD098.FluidBalance","FluidBalance_ALM",5000)
In this example, if the value of the DD098.FluidBalance tag exceeds 5000, the function returns a true value, indicating that the alarm was set; the alm_set variable will be set to true. Otherwise, the alm_set variable will be set to false.