Jump to Subroutine (JSR), Subroutine (SBR), and Return (RET)
The JSR instruction invokes another routine. When that routine completes, the execution returns to the JSR instruction.
The SBR instruction receives the input parameters passed by the JSR.
The RET instruction passes return parameters back to the JSR and ends the scan of the subroutine.
Available Languages
Ladder Diagram
FactoryTalk Design Studio
DSL - Ladder DiagramJSR(RoutineName,InputCount,InputPar,ReturnPar);
SBR(InputPar);
RET(ReturnPar);
Operands
IMPORTANT | Unexpected operation may occur if:
|
ATTENTION:
ATTENTION:
For each parameter in an SBR or RET instruction, use the same data type (including any array dimensions) as the corresponding parameter in the JSR instruction. Using different data types may yield unexpected results.Ladder Diagram
JSR Instruction
Operand | Data Type | Format | Description |
---|---|---|---|
Routine Name | ROUTINE | name | Subroutine to execute |
Input Par | BOOL SINT INT DINT LINT USINT UINT UDINT ULINT REAL LREAL structure | immediate tag array tag | Data from this routine to copy to a tag in the subroutine.
|
Return Par | BOOL SINT INT DINT LINT USINT UINT UDINT ULINT REAL LREAL structure | tag array tag | Tag in this routine to copy result from subroutine.
|
SBR Instruction
Operand | Data Type | Format | Description |
---|---|---|---|
Input Par | BOOL SINT INT DINT LINT USINT UINT UDINT ULINT REAL LREAL structure | tag array tag | Tag in this routine into which to copy the corresponding input parameter (maximum 40) from the JSR instruction. |
RET Instruction
Operand | Data Type | Format | Description |
---|---|---|---|
Return Par | BOOL SINT INT DINT LINT USINT UINT UDINT ULINT REAL LREAL structure | immediate tag array tag | Data from this routine to copy to the corresponding return parameter (maximum 40) in the JSR instruction. |
Affects Math Status Flags
No
Major/Minor Faults
A major fault will occur if: | Fault type | Fault code |
---|---|---|
JSR instruction has fewer input parameters than SBR instruction | 4 | 31 |
JSR instruction jumps to a fault routine | 4 | 990 or user-supplied |
RET instruction has fewer return parameters than JSR instruction | 4 | 31 |
Main routine contains a RET instruction | 4 | 31 |
Operation
IMPORTANT | Any routine may contain a JSR instruction but a JSR instruction cannot call (execute) the main routine. |
The JSR instruction initiates the execution of the specified routine, which is referred to as a subroutine:
- The subroutine executes each time it is scanned.
- After the subroutine executes, logic execution returns to the routine that contains the JSR instruction and continues with the instruction following the JSR.
To program a jump to a subroutine, follow these guidelines.
JSR
- To copy data to a tag in the subroutine enter an input parameter.
- To copy a result of the subroutine to a tag in this routine, enter a return parameter.
- Enter up to 40 inputs and enter up to 40 return parameters as needed.
SBR
- If the JSR instruction has an input parameter enter an SBR instruction.
- Place SBR instruction as the first instruction in the routine.
- For each input Parameter in the JSR Instruction, enter the tag into which you want to copy the data.
RET
- If the JSR instruction has a return parameter, enter an RET instruction.
- Place the RET instruction as the last instruction in the routine.
- For each return parameter in the JSR instruction, enter a return parameter to send to the JSR instruction.
- In a ladder routine, place additional RET instructions to exit the subroutine based on different input conditions, if required (Function block routines only permit one RET instruction).
Invoke up to 25 nested subroutines, with a maximum of 40 parameters passed into a subroutine, and a maximum of 40 parameters returned from a subroutine.
Tip: Select the Edit > Edit Ladder Element menu to add and remove variable operands. For the JSR and SBR instructions, add Input Parameter. For JSR and RET instructions, add Output Parameter. For all three instructions, remove Instruction Parameter. |
Execution
Ladder Diagram
Condition/State | Action Taken |
---|---|
Prescan | The rung is set to false. The controller executes all subroutines. To ensure that all rungs in the subroutine are prescanned, the controller ignores RET instructions (that is, RET instructions do not exit the subroutine). Input and return parameters are not passed. If the same subroutine is invoked multiple times, it will only be prescanned once. |
Rung-condition-in is false (to the JSR instruction) | N/A |
Rung-condition-in is true | Parameters are passed and the subroutine is executed. |
Postscan | Same action as Prescan |
Examples
Example 1
Ladder Diagram
Example 2
Ladder Diagram
Main routine
subroutine_1
Provide Feedback