Function Blocks
IntroductionFor the purposes of programmable controller programming languages, a function block is a program organization unit which, when executed, yields one or more values. Multiple, named instances (copies) of a function block can be created. Each instance has an associated identifier (the instance name), and a data structure containing its input, output and internal variables. All the values of the output variables and the necessary internal variables of this data structure persist from one execution of the function block to the next; therefore, invocation of a function block with the same arguments (input variables) does not always yield the same output values.Only the input and output variables are accessible outside of an instance of a function block, i.e., the function block’s internal variables are hidden from the user of the function block.In order to execute its operations, a function block needs to be invoked by another POU. Invocation depends on the specific language of the module calling the function block.The scope of an instance of a function block is local to the program organization unit in which it is instantiated.Declaration SyntaxThe declaration of a function must be performed as follows:FUNCTION_BLOCK FunctionBlockNameVAR_INPUTdeclaration of input variables (see the relevant section)END_VARVAR_OUTPUTdeclaration of output variablesEND_VARVAR_EXTERNALdeclaration of external variablesEND_VAR VARdeclaration of local variablesEND_VARFunction block bodyEND_FUNCTION_BLOCK
Declaring Functions in GF_ProjectVX CodeWhatever the PLC language you are using, GF_ProjectVX code allows you to disregard the syntax above, as it supplies a friendly interface for using function blocks.
Keyword | Description |
FunctionBlockName | Name of the function block being declared (note: name of the template, not of its instances). |
VAR_EXTERNAL .. END_VAR | A function block can access global variables only if they are listed in a VAR_EXTERNAL structuring element. Variables passed to the FB via a VAR_EXTERNAL construct can be modified from within the FB. |
Function block body | Specifies the operations to be performed upon the input variables in order to assign values to the output variables – dependent on the function block’s semantics and on the value of the internal variables.It can be written in any of the languages supported by GF_ProjectVX code |