User-defined functions
FactoryTalk Design Workbench
supports creating and managing user-defined functions (UDF) to reuse program logic and make our program more readable. Use a UDF for simple calculations that require one output.UDFs cannot access the local variables in the calling program. The local variables in the calling program must be passed to the UDF as input parameters.
IMPORTANT:
A user-defined input parameter cannot enable or disable UDFs because the input parameter only enables or disables the instructions inside the UDF.
To enable or disable a UDF, turn on or turn off
EN/ENO
in the Properties
pane.When
EN/ENO
is turned off, the UDF does not execute and the output parameters are not overwritten.If a UDF is executed multiple times per program scan, it is not recommended to use instructions that need more than one program scan to complete, such as timer, motion, message, and counter instructions. These instructions retain states between program scans.
Use UDFs instead of user-defined function blocks (UDFB)
- For a simple calculation that requires only one output, such asY=MX+B.
- For stateless instructions that do not require saving local variable values from execution to execution, such as SIN.
- When the output parameter does not require an array or structured data type.
- Whenever possible because a UDF consumes less memory.When multiple instances are required, using a UDFB might consume less memory than a UDF because a UDFB does not exist in a program until it is instantiated as a variable.
Use a UDFB instead of a UDF
- For complex calculations that have multiple outputs.
- When saving local variable values from execution to execution (saves state) is required.
- When multiple instances are required. Using a UDFB might consume less memory than a UDF because a UDFB in a project does not exist in a program until it is instantiated as a variable.
- When the output parameter requires an array or structured data type.
- When more than one message is sent simultaneously.When a UDF contains a message instruction such as MSG_CIPGENERIC, the UDF only sends one message at a time even if the UDF is called multiple times per program scan.
Consider the following best practices when using UDFs
- Use data encapsulation.UDFs can access global variables. It is not recommended to access global variables from a UDFB.
- Before calling a UDF multiple times in your project, debug the UDF program because:
- Monitoring the logic of a UDF when it is called multiple times is difficult.
- It is not supported to monitor a UDF that is called from an interrupt program.
- Initialize local variables each execution.By default, local variables in a UDF program are not automatically re-initialized each time the UDF is called. Because multiple instances are not supported for UDFs, it is recommended to initialize the UDF local variables each execution with the input parameters or a constant.
Provide Feedback