RETURN
You can use the RETURN statement within a Function Block (see Function Blocks in ST Programs). The RETURN statement terminates the Function Block but instructs the program to use the Function Block values that have been calculated up to that point. For example, if a Function Block calculates 4 output values and the RETURN statement is positioned after the second value has been calculated, the first 2 values will be used by the program but the 3rd and 4th values will not be used as the RETURN statement terminates the function block before they are calculated.
The RESULT statement is useful when certain values that are calculated by a Function Block are not required in a particular scenario. Enter the RETURN statement at the position where you want the program to terminate the Function Block (when a defined condition occurs).
Example:
- FUNCTION_BLOCK POWERLOAD
- VAR_INPUT
- CURRENT,POWER1,POWER2,POWER3:REAL;
- END_VAR
- VAR_OUTPUT
- OVERLOAD:BOOL;
- END_VAR
- IF((power1 * current) > 100) THEN
- overload := TRUE;
- RETURN;
- END_IF;
- IF((power1 * (current + 10)) > 100 THEN
- overload := TRUE;
- RETURN;
- overload := TRUE;
- END_IF;
- IF((power1 * (current + 20)) > 100 THEN
- overload := TRUE;
- END_IF;
- END_FUNCTION_BLOCK;
In this example, the Function Block is used to determine whether there is a power overload. If the power1 value x the current value is equal to or greater than 100, there is a power overload and so the overload value is true.
If the power2 value x the current value +10 is equal to or greater than 100, there is a power overload and so the overload value is true.
If the power3 value x the current value +20 is equal to or greater than 100, there is a power overload and so the OVERLOAD value is true.
Each IF statement needs to be performed in turn. There is a RETURN statement after the first IF statement and another after the second IF statement. This means that if the overload value is true after the first IF statement, the rest of the program is ignored. Similarly, if the overload value is true after the second IF statement, the rest of the program is ignored.