0

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.

Bryan Hdez
  • 23
  • 5

2 Answers2

2

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.

Steve
  • 963
  • 5
  • 16
0

I personally use this rule which I detailly described in my ST (ISBN 978-1-64199-106-3) book, when deciding what to create Function of FB.

Always create functions and create FB only when you cannot create function.

So, how do I know when I cannot create function? There are only 2 reasons.

  1. If inside your POU you use another FB as a local variable create FB. For instance, if you use a timer or counter inside your POU, then it should be FB.

  2. If inside your POU you want to remember the value of any local variable from one call to another. For instance you create counter and increace local INT variable crom call to call.

Sergey Romanov
  • 2,949
  • 4
  • 23
  • 38