Function (FC) syntax is quite similar to the FB syntax. Still there are some key differences. I'll talk about these in a minute.
Example:
The FC definition starts with the FUNCTION keyword followed by the quoted name of the FC.
Here we face the first difference: FCs have a return type. The return type (LREAL in this case) is separated with a colon. The function is expected to provide some value on return and this is the way to do it. If no value is returned the return type should be VOID.
The return value is provided in a special variable named after the function. The type of this variable matches the type of the function. This special variable is defined implicitly for each function. It is there. Just use it. If you don't initialize it the return value is undefined.
Like the function block (FB) the FC may have block/system attributes assigned. Then come the variable definitions.
Here we face the second difference: The FC cannot have static variables. Weird right? No. It is in fact quite natural. The FC stores its state on the stack so once it returns all variables disappear. So as a general rule: Static variables in a FC are treated like temporary ones!
Now how do you call a function? It depends ...
Custom functions like this one are called via their quoted name ("FACTORIAL" in this case). Standard (built-in) functions however do not need quotes (example: SIM_TIME())
Now you know how to work with functions. So we move on ...
Comments