11 - 80 11 - 80
MELSEC-Q
11 INSTRUCTIONS AND FUNCTIONS
GOSUB RETURN
Instruction
GO to SUBroutine/RETURN
• Branches to a subroutine and returns to the specified line number.
GOSUB <line number 1>
RETURN [ <line number 2> ]
Syntax
line number 1
• • • •
Specify the starting line number of the subroutine to
be branched.
line number 2
• • • •
Specify the line number to return.
Examples
GOSUB 100
• • • •
Branches to the subroutine of line number 100.
Description
• The GOSUB to RETURN instruction branches to the subroutine by GOSUB, then returns
to the instruction after the GOSUB instruction, specified line number, or label by the
RETURN instruction.
• <line number> can also be specified by a label.
• The same subroutine can be called many times in a program.
• Subroutines can be nested as long as the memory capacity allows.
An “Out of Memory” error occurs when the memory runs out.
10
500
GOSUB1000
1000
1500RETURN
GOSUB2000
2000
2500RETURN
• The RETURN instruction returns the control to the following instruction of the GOSUB
instruction that branched to the subroutine.
• While multiple RETURN instructions can be specified within one subroutine, they have to
correspond to GOSUB instructions correctly.
• A subroutine must be used by a GOSUB instruction. If it is executed by itself, a “RETURN
without GOSUB” error occurs when the RETURN instruction is executed.