Search code examples
plctwincatstructured-text

Is there a difference when using function block rather than functions in TwinCat?


I started making the structure for a new PLC program, but I was wondering when is convenient to use a funtion rather than a function block, if there's any benefit on using them in TwinCat.


Solution

  • The defining characteristic is that a FUNCTION does not have internal memory of its variables.

    • FUNCTIONs do not need to be instantiated before use
    • FUNCTIONs re-instantiate their memory every time they are called

    In contrast:

    • FUNCTION_BLOCKs need to be instantiated before use ( to allocate their memory space )
    • FUNCTION_BLOCKS will retain the status of their internal variables

    This pretty much describes why you would use one over the other.

    If you are writing a repeated segment of code that doesn't require memory of its previous state use a FUNCTION. Otherwise use a FUNCTION_BLOCK.