Program VS Function Block VS Functions

Sham

Member
Join Date
Sep 2019
Location
Australia
Posts
152
Hi All,

I know this is very basic and I do have a concept, but I want to still ask you all:
What is the difference between a function block, a function and a program?
I kind of fail to understand the difference between function block and function mostly. Please shed some light.
 
Which software, Codesys?

In Siemens Function block has its own data that can persist from one scan to the next and even be retained after power recycle.

Function does not have persistent data so it is used to do some task and at most return a value or output but the data is lost between scans.
CodeSys is similar and "Program" though has data, it can NOT be called more the once, unlike FB and FC which can be called many times. When calling FB multiple times you have the choice of using different data for each instance or the same data meaning the data could change between one instance and the next.

Variables in CodeSys also play a role as to how accessible they are from outside an FB.
 
Last edited:
Which software, Codesys?

In Siemens Function block has its own data that can persist from one scan to the next and even be retained after power recycle.

Function does not have persistent data so it is used to do some task and at most return a value or output but the data is lost between scans.
CodeSys is similar and "Program" though has data, it can NOT be called more the once, unlike FB and FC which can be called many times. When calling FB multiple times you have the choice of using different data for each instance or the same data meaning the data could change between one instance and the next.

Variables in CodeSys also play a role as to how accessible they are from outside an FB.
You have stated the definition of a function block for IEC 1131-3. Most PLCs use this definition. I know I do.
 
A Function can have a number of input variables but only returns one variable
so for example you want a Function to add 3 variables and return the result.
The interface allows you to create variable inputs & local variables (these are used internally only), so you configure the interface like
My_Var_1
My_Var_2
My_Var_3
And the return result type i.e. integer.
The code internally to Add the three variables passed to the function is
Result = My_Var_1 + My_Var_2 + My_Var_3
on returning from the function the result is passed to the variable you code on the result.
A Function block is almost the same, however, it allows you to configure not only input variables but many output variables and In/Out variables (these can be passed to the function block and return values).
Calling either a function or function block can be as a single instance or multiple instance i.e. calling the Fun/FB many times but giving it the same instance name means it passes your IN variables to the function/FC/FB and jumps to a single instance of the code, processes the code and returns any OUT variables back to the program where it left off.
Giving the FC/FB different instances means it creates a separate bit of code for each call so in effect increases the amount of code.
If you think of it as machine code sort of, this is what it would look like
Program.....
Call FB My_FB // Call the FB/FC
Move Var_1 to My_Var_1 //Move your data to the FB/FC internal memory
Move Var_2 to My_Var_1 // Do the same for any other input or In/Out variables
Jump to FB/FC code
My_Result = Result // return the result to the variable passed as a reference i.e. My_Var_3
........ //rest of program block


END // End of program

My_FB / / label jumped to for FB/FC
Result = My_Var_1 + My_Var_2 + My_Var_3
Return // Return to the main program

So basically when you call an FB/FC it passes your variables to the FC internal variables, jumps to the FB/FC code, processes it and returns any out or IN/OUT temp variables to your variables.
 
It is somewhat hard to give a clear definition because there may be differences between several programming environments. For instance, codesys functions can, just like function blocks, have IN, IN_OUT and OUT parameters and thus can output multiple values, even though the "function result" is always a single values.

For me the main distinction between functions and function blocks is that function blocks can maintain state between two calls. For that reason, different calls in code require different instances of the function block. So the function blocks need to be in a declaration section. Declaring a function block seems arbitrary at first sight, but is required precisely because every instance maintains state and for it to do that it requires it's own piece of memory.

For instance a TON timer must be a function block rather than a function because it must "remember" the start time of the HIGH state of the input and compare it to the current time at every call of the function block. Another timer in another part of the code may have a different start time and duration. Therefore it needs a separate memory location to store it's own start time and compare against the current time at every call of the TON function block.

A piece of code that typically does not maintain state between calls is a mathematical operation. You might create a piece of code that adds three integer values and returns the result as an integer. While technically you could write a function block that does just this, that would not be my choice. An instance would need to be declared before I can use it. One addition of three values has nothing to do with the next addition of three values. A typical case where I would write a function rather than a function block. No need to declare an instance, I can call it as many times as I want everywhere in my program and one call has absolutely nothing to do with the next one. It does not "maintain state information" between calls.
 
I agree, however, I was trying to explain, In the normal sense (programming in most languages), however, PLC mfg seem to do their own thing and not follow the rules. anyway, many years ago I created a function block in an SLC, and before you say it is not possible in a roundabout way it is. Here is the explanation.
I had an SLC505 system that collected data from 14 production lines both using communication and digital I/O (mainly for the ones that I could not modify i.e. sensors & digital signals added to the machines to gather data).
The idea was to grab data like machine running/stopped time, line speed, pack counts, packs per min, rejects etc and send the data to each display above the lines, this consisted of 14 displays that would display this for each line.
It became apparent that to convert the data into an ascii string & send this to each display would take up so much code that it would run out of space, the answer was to create a standard block of code that took the data, converted it into the ascii string with headers, footers & checksum & send it to the coms card.
to do this I allocated an area of an integer file to use as a scratchpad so this area became the input & output variables in the function block.
So for example the input variables would be N7:50 to N7:59 and the output variables were N7:60 to N7:69.
I created a program file that used these variables to do the required conversion to ascii & generate the headers/footers, check sum etc.
then in the main program before calling the block I would transfer the values from the variables to the temps and then call the program block and then transferring the out variables back to the main program, this was done 14 times by polling i.e. a counter that was triggered on a com send complete.
Attached is a simple way of doing this in RSL note: this is just a simple idea and would not be practicable as it takes more code to pass the variables and call the function than actually doing it long hand, however, it shows the principal. so basing it on the simple add function in an earlier post in the main prog lad 2 it passes data from N7:0 to N7:2 to the temps N7:50 to N7:52 jumps to LAD 6 this does the calc & on return from the subroutine returns the result from N7:53 to N7:3 on the first call and N7:7 on the second.
You can see from this it is possible to create a standard (AOI) although a bit of a pain it means on a complicated function it could save program memory and a lot of typing or copy/paste etc.
As I said earlier, this is just a simple example to demonstrate it working but by the use of indirect addressing etc and a need for a complex function it will work on older types of PLC's that have program block control but no function blocks.
And before someone comes up with scan time, the way the (FB) was called conditionally in the one I did some years ago the call was conditional on completing the com before it called the block again so probably only called the block every few scans. After all this is exactly how the compiler will produce the code by passing the variables to/from the blocks own variables.
Incidentally, although I did not calculate the memory saved but when I originally did the project I estimated it saved between a half and two thirds of the required memory compared to long hand coding.
 
A Function can have a number of input variables but only returns one variable
so for example you want a Function to add 3 variables and return the result.
The interface allows you to create variable inputs & local variables (these are used internally only), so you configure the interface like
My_Var_1
My_Var_2
My_Var_3
And the return result type i.e. integer.
The code internally to Add the three variables passed to the function is
Result = My_Var_1 + My_Var_2 + My_Var_3
on returning from the function the result is passed to the variable you code on the result.
A Function block is almost the same, however, it allows you to configure not only input variables but many output variables and In/Out variables (these can be passed to the function block and return values).
Calling either a function or function block can be as a single instance or multiple instance i.e. calling the Fun/FB many times but giving it the same instance name means it passes your IN variables to the function/FC/FB and jumps to a single instance of the code, processes the code and returns any OUT variables back to the program where it left off.
Giving the FC/FB different instances means it creates a separate bit of code for each call so in effect increases the amount of code.
If you think of it as machine code sort of, this is what it would look like
Program.....
Call FB My_FB // Call the FB/FC
Move Var_1 to My_Var_1 //Move your data to the FB/FC internal memory
Move Var_2 to My_Var_1 // Do the same for any other input or In/Out variables
Jump to FB/FC code
My_Result = Result // return the result to the variable passed as a reference i.e. My_Var_3
........ //rest of program block


END // End of program

My_FB / / label jumped to for FB/FC
Result = My_Var_1 + My_Var_2 + My_Var_3
Return // Return to the main program

So basically when you call an FB/FC it passes your variables to the FC internal variables, jumps to the FB/FC code, processes it and returns any out or IN/OUT temp variables to your variables.


But on Siemens PLC FC blocks can have multiple outputs instead of one return?
 
Which PLC? It will make a big difference. In Oz there is very little Codesys by the way. Omron, AB, Schneider mainly.
 
Most PLC manufacturers are going to FBD ST programming using IEC standards so most PLC's will use these standards, however, it does appear that the interpretation of the standards seem to vary slightly. The original (and probably still is) idea is to have source code that is transferable across platforms (not that the PLC manufacturers seem to want this), to be honest it has not worked out that well as PLC's will have their own standards for certain specialty cards but in general for normal logic processing the idea is that any IDE will be able to convert from one platform to another with little or no modification.
Some have gone all the way with new hardware etc. to make this a reality, however, some have modified their IDE's to cater for both, in reality this means the compiler just generates the code into the same mnemonics as the original ladder for example in the older ladder a timer is a real software timer whereas in the IEC instructions they have more functionality very much like the Rockwell where not only do you have the coil & contact (.DN) instruction but the current value etc. so some IDE's to get round this problem and make it compatible will use a timer built out of standard logic i.e. instead of a fixed address of timers & their components it uses for example a variable word & variable bits.
An example is Mitsubishi, in conventional timers they are fixed addresses i.e. T0 to Txxx and only the contact & pre-set variables
To get the functionality of an IEC timer when you call the block it actually creates code that does not use the built in timers, instead it uses "D" variables i.e. D998 as the pre-set D997 ,as the current, M998 as the done bit etc. (these variables are determined at compile time) so in effect it does not use the on-board timers at all but a subroutine (or FB) to generate the code functionality. I suspect the IEC was based on the original Step5 as Siemens had FB's etc before IEC was developed, Siemens was far ahead of others and the memory in an S5 PLC was in the format of a hard disk (or floppy in the early days), The blocks like PB's, FB's DB's were placed in memory and header information on it's address space stored in a table like a File Allocation table, when a program block is written to the PLC the block is placed into spare memory, after or before the main program scan the FAT table is modified i.e. the old information for that block is removed and replaced with the location of the new block. The old block was not deleted so in the early days you had to do a compress of the memory to remove these, later, this was done automatically this meant it was possible to download a whole program while in run mode. I worked with an Ex. Siemens engineer who knew the S5 system intimately, he showed me some instructions not generally released in their manuals. He even showed me how to write a self modifying program (very dangerous but it worked).
 
Thank you everyone for your help! So what I have understood is :

Functions are used where we dont need to retain values, whereas function blocks are used where we need to retain the values? And a program is kind of a fb, but it cannot be instantiated (cannot be used more than once in the project)?
 
Apart form the latest PLCs Omron has functions - ie timers, comparators, counters, arithmetic functions.
Also have function blocks that can use the functions and can be written in ladder or ST.
Both can have retentive and non-retentive data in them - depends on how you program them.
Program is the whole thing.
Confused? They are all doing different things and with very few exceptions nothing is portable IEC or not.
Oh - another twist - with these PLCs you can opt for auto I/O generation or use the old numbered system.
I like the old numbered system - just type in a number and go - no need to look for bloody symbols - way too slow.
Needless to say I do not opt for auto generation cause I would have to find bloody symbols - numbers are so much easier if you know where you are going.
Many will disagree but I can generally write software way quicker this way in ladder than FBs, ST and all the rest and having to find symbols.
Just me - quicker - make more money - simple as that.
 
Last edited:

Similar Topics

Hi, I'm learning GX Works 2 for upcoming project. I want to use ST Function Blocks in Ladder Program and it works if the FB(Structured text)...
Replies
5
Views
1,583
So I have this situation where I need to debug a major change to a program for a machine. I have very limited time on the actual machine and it is...
Replies
24
Views
8,999
Siemens S7-1500 I am trying to write some logic I just can't quite wrap my head around the cleanest way to write it, and was hoping for some...
Replies
5
Views
2,414
Hi Forum, I was wondering if there are PLC IDEs which have a function which allows 2 PLC programs (in either ladder code or compiled) to be...
Replies
10
Views
3,994
Hello I'm having an issue with a SLC instruction in a SLC 5/05 PLC program In the screenshots below I have the "Scaled Source" The scaled source...
Replies
20
Views
7,531
Back
Top Bottom