Subroutine v Function Block?...
Highland Controls said:
I am looking for some expert advice. I have always made function blocks both with inputs and outputs and without inputs or outputs. I make the ones without inputs and outputs as a way to organize code. I have a program that is called from a task, and within the program I call these function blocks. I have been told that this isn't good practice, and that I should make each of these function blocks a program...
...I have also made function blocks...that can only be used within the project...I know this isn't recommended, but it is a good was to organize code...
As you have rightly referenced the IEC 61131-3 Standard, I will too...
I posted this not too long ago here on the Forum...
Geospark said:
...The IEC 61131-3 software model defines three types of Program Organization Units:
1. Programs
2. Function Blocks
3. Functions
...
Programs, Function Blocks and Functions are designed to facilitate the organization of a logical "Task" i.e. series of instructions which performs a Task, or part of a Task. Therefore, Programs, Function Blocks and Functions are all classified under the Standard as a POU - Program Organization Unit. This is what the Standard intended them to be used for and that they be complimentary to each. The above are listed in descending order of functionality where the Program is the higher-level organizational unit which contains the Subroutines or Function Blocks and Functions required to perform the given Task.
Function Blocks are primarily used for bespoke, tailored and unique blocks of repetitive or convoluted code, whether small or large, to be used or called from within Programs, where the Programs more so tend to contain the higher level logical structures for the given Task. Function Blocks are often used to perform relatively smaller, but still important, sub-tasks or Subroutines within a Program, within a given Task, but may also be used to contain the equivalent of whole Subroutines. Function Blocks and Subroutines are generally interchangeable or comparable with notable similarities and differences. For instance, Function Block calls are often more efficient in their execution than Subroutines, but Function Blocks may often use more memory than comparable Subroutines.
My own breakdown of the three types of POU above, and how they are primarily implemented, would be -
1. Programs - used for high-level organization of Function Blocks and/or Subroutines for a given Task
2. Function Blocks - used for mid-level organization of logic for a given Program for a given Task
3. Functions - used for low-level organization of logic for a Function Block and/or Subroutine for a given Program for a given Task
There is nothing right or wrong with the use of Function Blocks as a relatively large or complex alternative to a Subroutine within a Program. Users have found good and varied uses for Function Blocks in consolidating repetitive, multi-call Subroutine equivalence within Programs. However, the use of Function Blocks to organize whole Programs to perform a given Task, or part of a larger Task, or even multi-Task projects, is not really advisable and not really what they were designed to do. They are intended to be used as a POU for organizing functional blocks of logical code rather than entire Programs.
But, is that what you have been doing, or?...
If the individual(s) advising to use a "
program" (as underlined in your above quote) instead of using a Function Block actually means or meant that you should use a "Subroutine" instead of a Function Block, then in all essence Subroutines and Function Blocks at the programming level are pretty much the same thing and they are really just advocating one type of "Subroutine" over another. While that is a healthy debate in itself (which to use, when and where?), I cannot agree with anyone blanket advising that you should always use one or the other method of encapsulating blocks of logical programming code.
The only blanket advise I always give is this -
Neither is correct until you carefully consider the application at hand and which program organizational method may suit best. And knowing which may suit best is only possible once you know at least some of the differences between each method i.e. when to use a Subroutine and when to use a Function Block and when might it not matter either way?
For some, they don't necessarily follow that application dependency "rule" and may tend more to have a personal programming preference. That's fine too, if it suits the types of applications they tend to work on, or if it really does not matter either way.
So I cannot really say if the individual(s) advising you are firstly, actually referring to using Subroutines over Function Blocks, and if so then secondly, basing that advice on their personal and general preferences, or if they are actually advising you based on a specific application of yours, or type of application you tend to develop?
In a nutshell...
Subroutine v Function Block may be a valid debate here, but Program v Function Block is not valid, in my book, and the Standard's book for that matter.
Regards,
George