RETURN¶
The RETURN statement causes an early exit from the current program
organization unit.
IEC 61131-3 |
Section 3.3.2.5 |
Support |
Supported |
Syntax¶
RETURN ;
Description¶
RETURN terminates execution of the current program, function, or function
block. In a function, the return value is the last value assigned to the
function name before RETURN executes. In a program or function block,
execution resumes at the caller.
Example¶
FUNCTION Divide : DINT
VAR_INPUT
numerator : DINT;
denominator : DINT;
END_VAR
IF denominator = 0 THEN
Divide := 0;
RETURN;
END_IF;
Divide := numerator / denominator;
END_FUNCTION
PROGRAM main
VAR
safe_result : DINT;
normal_result : DINT;
END_VAR
safe_result := Divide(10, 0);
normal_result := Divide(10, 3);
END_PROGRAM
Conditional Return (Language Extension)¶
Note
This is an extension not part of the IEC 61131-3 standard. See Enabling Dialects and Features for how to enable it.
Pass the --allow-conditional-return flag to enable it, or select a dialect that includes it. See
Enabling Dialects and Features for the dialects and flags
reference.
RETURN ( expression ) ;
RETURN(condition) returns only when condition is TRUE. When it is
FALSE, execution goes on with the next statement. It means the same as:
IF condition THEN
RETURN;
END_IF;
The condition must be a BOOL; another type is an error
(P4072). The condition is never
the return value of a function, even a BOOL function: the function
returns the last value assigned to its name, as with a plain RETURN.
Without the flag, RETURN(condition) is an error
(P4098).
FUNCTION Divide : DINT
VAR_INPUT
numerator : DINT;
denominator : DINT;
END_VAR
Divide := 0;
RETURN(denominator = 0);
Divide := numerator / denominator;
END_FUNCTION
PROGRAM main
VAR
safe_result : DINT;
normal_result : DINT;
END_VAR
safe_result := Divide(10, 0);
normal_result := Divide(10, 3);
END_PROGRAM
See Also¶
EXIT — break from innermost loop
FUNCTION — function definition
FUNCTION_BLOCK — function block definition