So the function blocks need to be in a declaration section.
#The difference between the original ladder logic program and the one running in the plc code
For that reason, different calls in code require different instances of the function block. 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.įor me the main distinction between functions and function blocks is that function blocks can maintain state between two calls. It is somewhat hard to give a clear definition because there may be differences between several programming environments. 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. My_Result = Result // return the result to the variable passed as a reference i.e. Move Var_2 to My_Var_1 // Do the same for any other input or In/Out variables Move Var_1 to My_Var_1 //Move your data to the FB/FC internal memory If you think of it as machine code sort of, this is what it would look like 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. 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.
On returning from the function the result is passed to the variable you code on the result.Ī 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).Ĭalling either a function or function block can be as a single instance or multiple instance i.e. The code internally to Add the three variables passed to the function is The interface allows you to create variable inputs & local variables (these are used internally only), so you configure the interface like So for example you want a Function to add 3 variables and return the result. A Function can have a number of input variables but only returns one variable