Jump to Subroutine (JSR), Subroutine (SBR), and Return (RET)

This information applies to the
CompactLogix
5370,
ControlLogix
5570,
Compact GuardLogix
5370,
GuardLogix
5570,
Compact GuardLogix
5380,
CompactLogix
5380,
CompactLogix
5480,
ControlLogix
5580, and
GuardLogix
5580 controllers.
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
JSR_LD_avail_v31
SBR_LD_avail_v31
RET_LD_avail_v31
Function Block
JSR_FB_avail_v31
SBR_FB_avail_v31
RET_FB_avail_v31
Sequential Function Chart
SBR_RET_FC_avail_v31
Structured Text
JSR(RoutineName,InputCount,InputPar,ReturnPar);
SBR(InputPar);
RET(ReturnPar);
Operands
IMPORTANT: Unexpected operation may occur if:
  • Output tag operands are overwritten.
  • Members of a structure operand are overwritten.
  • Except when specified, structure operands are shared by multiple instructions.
WARNING:
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
CompactLogix
5370,
ControlLogix
5570,
Compact GuardLogix
5370, and
GuardLogix
5570 controllers
Data Type
CompactLogix
5380,
CompactLogix
5480,
ControlLogix
5580,
Compact GuardLogix
5380, and
GuardLogix
5580 controllers
Format
Description
Routine Name
ROUTINE
ROUTINE
name
Subroutine to execute
Input Par
BOOL
SINT
INT
DINT
REAL
structure
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.
  • Input parameters are optional
  • Enter a maximum of 40 input parameters, if needed.
Return Par
BOOL
SINT
INT
DINT
REAL
structure
BOOL
SINT
INT
DINT
LINT
USINT
UINT
UDINT
ULINT
REAL
LREAL
structure
tag
array tag
Tag in this routine to copy result from subroutine.
  • Return parameters are optional
  • Enter a maximum of 40 return parameters, if needed
SBR Instruction
Operand
Data Type
CompactLogix
5370,
ControlLogix
5570,
Compact GuardLogix
5370, and
GuardLogix
5570 controllers
Data Type
CompactLogix
5380,
CompactLogix
5480,
ControlLogix
5580,
Compact GuardLogix
5380, and
GuardLogix
5580 controllers
Format
Description
Input Par
BOOL
SINT
INT
DINT
REAL
structure
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
CompactLogix
5370,
ControlLogix
5570,
Compact GuardLogix
5370, and
GuardLogix
5570 controllers
Data Type
CompactLogix
5380,
CompactLogix
5480,
ControlLogix
5580,
Compact GuardLogix
5380, and
GuardLogix
5580 controllers
Format
Description
Return Par
BOOL
SINT
INT
DINT
REAL
structure
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.
JSR_SBR_RET
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.
JSR_ex
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
Function Block
Condition/State
Action Taken
Prescan
See Prescan in the Ladder Diagram table.
EnableIn is false
N/A
EnableIn is true
Parameters are passed and the subroutine is executed
Instruction first run
N/A
Instruction first scan
N/A
Postscan
See Postscan in the Ladder Diagram table.
Structured Text
Condition/State
Action Taken
Prescan
See Prescan in the Ladder Diagram table.
Normal Execution
Parameters are passed and the subroutine is executed.
Postscan
See Postscan in the Ladder Diagram table.
Examples
Example 1
Ladder Diagram
JSR_SBR_RET_LD_ex_v31
Structured Text
Routine
Program
Main routine
JSR(routine_1,2,value_1,value_2,float_value_1);
Subroutine
SBR(value_a,value_b);
<statements>;
RET(float_a);
Example 2
Ladder Diagram
Main routine
JSR_LD_ex_v31
subroutine_1
RET_LD_ex.png_v31
Example 3
Function Block
L5K_JSR_SBR_RET_FB_ex_v31.png
Provide Feedback
Have questions or feedback about this documentation? Please submit your feedback here.