plctwincatstructured-text

What is the difference between "Action" and "Method" in TwinCAT 3?


I see options to make an action instead of a method when using function blocks in TwinCAT 3. I am only familiar with methods. What is the difference between the two, and when should you use one instead of the other?

I would like to know why the FIFO data structure in TwinCAT uses actions and not methods.


Solution

  • Methods allow you to declare variables which are only avaliable from inside the method. These variables are temporary and do not retain their value over multiple calls. Also, methods can have input and output variables which can be used to pass data to the method on a call and jield a result.

    Actions do not allow you to declare variables or define inputs or ouptputs. You can however acces and modify the data of the owning function block or programm (which is also possible in methods).

    So basicalliy, methods provide more functionality than actions. Besides that, both are pretty similar. There are different reasons to use or not use actions over methods when aplicable. Such as making clear that no data transfer is happening during a call by using an action or simplifying your code structure by never using actions but only methods even when actions would suffice.

    If I am not mistaken, actions existed before methods were implemented in TwinCAT. This might be another reason, why the FIFO structure uses actions.