Program control (output instruction)
RET: Return
Ends the current routine early and returns to the caller.
Also called: return from subroutine, early return.
RET()What it does
When RET's rung is true, the rest of that routine is skipped and execution carries on in whatever called it. It ends one routine, not the scan: TND is the instruction that ends the scan.
The pattern it exists for is the guard clause. A routine that only makes sense when the machine is ready can test that once at the top and return, instead of repeating the same permissive contact on every rung below. The routine gets shorter and, more usefully, the condition is stated once so it cannot be forgotten on the rung somebody adds next year.
What it does not do is turn anything off. Rungs after the RET are not scanned at all, so any output they drive keeps the state it was left in. An output that must be off when the routine bails out has to be driven off before the RET, or from a rung that always runs.
A rung you can run
A guard clause at the top of a routine
ROUTINE Main
RUNG
XIC(Auto_Mode)JSR(AutoSeq)
RUNG
XIC(Cycle_Run)XIC(Machine_Ready)OTE(Conveyor)
ROUTINE AutoSeq : SUBROUTINE
RUNG
XIO(Machine_Ready)OTU(Cycle_Run)
RUNG
XIO(Machine_Ready)RET()
RUNG
XIC(Start_PB)OTL(Cycle_Run)
RUNG
XIC(Stop_PB)OTU(Cycle_Run)With Machine_Ready off, the routine drops the cycle and returns, so the start button below it can do nothing at all. Turn Machine_Ready on and the same button works. Notice that the conveyor is driven in Main rather than after the RET: that is deliberate, and it is what stops it freezing on the moment the routine bailed out.
Run this rung in the simulatorWhat it is called on other controllers
| Platform | Equivalent |
|---|---|
| Rockwell (Studio 5000) | RET |
| Siemens (TIA Portal) | RET |
| Mitsubishi (GX Works) | SRET |
| CODESYS and IEC 61131-3 | RETURN |
Common mistakes
- Expecting outputs below the RET to drop out. They freeze at their last value instead, which is worse than either being on or off, because nothing in the program is driving them any more. Unlatch what needs to be safe before you return.
- Confusing it with TND. RET ends this routine and the scan continues; TND ends the whole scan, including every routine after it.
Practise it
Interactive exercises that use RET. Each one checks your ladder against a real scan.
