<< Click to Display Table of Contents >> Navigation: »No topics above this level« Functions (IEC ST) |
mapp Services V5.16
The definition of a function includes its implementation. A declaration without implementation is used to map external functions.
VAR_INPUT
x : REAL;
y : REAL;
END_VAR
(* Start of implementation *)
Hypotenuse := SQRT(x ** 2 + y ** 2);
END_FUNCTION
The scope of declared functions is determined according to the file extension and the most recent #pragma SCOPE.
Functions support return values of basic types, data types derived from basic types, enumerations, structures, arrays and string types. Data type NONE can be used for functions with no return value.
The function can return structured types. Such a function can be used for inlining values for the target position.
FUNCTION P : PointType
VAR_INPUT
X : LREAL;
Y : LREAL;
Z : LREAL;
END_VAR
P.Pos.X := X;
P.Pos.Y := Y;
P.Pos.Z := Z;
END_FUNCTION
PROGRAM _MAIN
MovePoints(P(300, 200, 100));
END_PROGRAM
The return value of a function is reinitialized to the default value of its return type.
TYPE
Bar : DINT := 100; (* Default initial value 100 *)
END_TYPE
FUNCTION Foo : Bar
(* If Foo not assigned, 100 is returned *)
END_FUNCTION
VAR_INPUT arguments of basic types can be omitted in the function call. If omitted, then the default values are used.
Arguments of the following types cannot be omitted:
•VAR_INPUT: Strings
•VAR_INPUT: Arrays
•VAR_INPUT: Structures (including function blocks)
•VAR_INPUT: REFERENCE TO <Any type>
•VAR_IN_OUT: <Any type>
Example
FUNCTION myFn : REAL
VAR_INPUT
x : REAL := 1;
y : REAL := 0;
END_VAR
myFn := x + y;
END_FUNCTION
The following calls are valid:
Function call |
Argument values |
Description |
myFn(4, 3); |
4 + 3 = 7 |
All arguments are unnamed. |
myFn(x := 4, Y:= 3); |
4 + 3 = 7 |
All arguments are named. |
myFn(x := 4, 3); |
4 + 3 = 7 |
Mixed naming call. |
myFn(x := 4); |
4 + 0 = 4 |
Default value for y. |
myFn(y := 1); |
1 + 1 = 2 |
Default value for x. |
myFn(); |
1 + 0 = 1 |
Default values for all arguments. |
Unbound strings are strings without an explicitly defined dimension. Unbound strings are permitted only as function arguments and function return value types.
An unbound string argument is bound to an actual string variable during the function call. The assignments to unbound string arguments are therefore always checked against the maximum string size.
References to unbound stings and arrays of unbound stings are not permitted. Using unbound strings for variable declarations or type declarations is not possible.
Using function ADR on an unbound string argument returns the address of the bound string.
Example
// Convert lowercase string to uppercase
FUNCTION ToUpper : DINT
VAR_IN_OUT
s : STRING;
END_VAR
VAR
i : INT;
c : REFERENCE TO USINT;
END_VAR
FOR i := 0 TO LEN(s)-1 DO
c ACCESS ADR(s) + i;
IF c >= 97 AND c <= 122 THEN
c := c - 32;
END_IF
END_FOR
END_FUNCTION
VAR
S : STRING[128];
END_VAR
PROGRAM _MAIN
S := 'Hello, World!';
ToUpper(S);
END_PROGRAM
Named function arguments are supported. The name of the argument must be the same as the associated parameter name. The order in the argument list must always reflect the order in the parameter list.
Example
FUNCTION P: PointType
VAR_INPUT
X : LREAL;
Y : LREAL;
Z : LREAL;
END_VAR
P.Pos.X := X;
P.Pos.Y := Y;
P.Pos.Z := Z;
END_FUNCTION
PROGRAM _MAIN
MovePoints(P(X:=300, Y:=200, Z:=100));
END_PROGRAM
External functions are implemented outside the interpreter on the PLC (AS library or user library function). This type of function can be called from a program after it has been declared. The declaration is always assumed to be correct since it cannot be checked. If an interpreter implementation exists for the same declaration, then the interpreter implementation will be called. Use #pragma CALL_MODE to call the external function.
Example of declaring and calling function clock_ms from library AsTime. The prototype of the function was copied from file AsTime.fun. Language extension {REDUND_OK} is ignored.
{REDUND_OK} FUNCTION clock_ms : TIME (* Provides a continuous millisecond counter in data type TIME *)
END_FUNCTION
VAR
time : TIME;
END_VAR
PROGRAM _MAIN
time := clock_ms();
END_PROGRAM
Functions cannot normally access variables defined outside the function. To access a global variable, the variable name must be prefixed by a double colon.
VAR
TestVariable : DINT;
END_VAR
FUNCTION getTestVariableValue : DINT
(* Returns the value of TestVariable. *)
getTestVariableValue := ::TestVariable;
END_FUNCTION
Topics in this section: