A function is a stateless POU, in other words, if called with the same parameters, the result is always the same; a function has no inner state variables that would keep values between the calls. A function returns one result only (however, the results may be of different types). A function which returns no result should not be called in an expression. The norm describes standard functions, and user-defined functions. Standard functions are e.g. ADD for adding, ABS for absolute value, SQRT for square root, SIN for sine function. New user defined functions may be used repeatedly.
A function consists of two parts: declarative and executive. In the declarative part, all variables necessary for the operation of the function are defined. The executive part contains the commands which produce the function's result.
FUNCTION name: data_type ... END_FUNCTION //data type is not compulsory for a function not returning a value
The declarative part of a function contains definitions of variables which a function needs for its activity. The variables are used for storage of information. Each variable is defined by a variable name, and data type. The data type is specifying the size of the occupied place in the memory, and determines the way the variable is processed (to a certain extent). For variables definition, standard data types are available (BOOL, BYTE, INT, …). Usage of these types depends on what information is going to be stored in the variable (e.g. BOOL for yes/no information, INT for signed integer numbers, etc.). User is allowed to define his/her own data types. The location of variables in memory is managed by the Idekit Studio automatically. If necessary, the location in memory may be user-defined, too.
VAR_INPUT ... END_VAR
VAR_OUTPUT ... END_VAR
VAR_IN_OUT ... END_VAR
VAR ... END_VAR
VAR_EXTERNAL ... END_VAR
Assignment of initial values to the variables
VAR I: INT := 1; END_VAR;
The executive part follows the declarative part. It contains commands and instructions. In exceptional cases, the function definition may not contain the declarative part, and the executive part is right behind the definition of function beginning. As an example, it may be a function which works with global variables only - this is not an ideal solution, although it may exist. The executive part of a function may contain calls of other functions. Parameters for the called functions or function blocks may be handed over at the call.
MY_FUNC := Value; //function's result
A := MY_FUNC( First_Index := 1, Last_Index := 5 );
A := MY_FUNC( 1, 5);
Example of a function (MY_FUNC):
VAR_GLOBAL //external interface
Data_Array: ARRAY[0..100] OF INT;
FUNCTION MY_FUNC: INT //function name "MY_FUNC", returns a result of type INT
VAR_INPUT //input variables
First_Index, Last_Index: INT;
VAR_EXTERNAL //external variables
Data_Array: ARRAY [0..100] OF INT;
VAR //local variables
Sum, I: INT;
(* Function body *)
Sum := 0;
FOR I:= First_Index TO Last_Index
DO Sum := Sum + Data_Array[I];
MY_FUNC := Sum; //function result
A user-defined function inserted in a FUPLA program.
The input variable en is used for concatenation and blocking of subsequent calculations. The variable "eno" copies the state of the input variable "en".