GoSub...Return Statement

See AlsoXOIUT9              ExampleTGRK0C>Low

Branch to, and return from, a subroutine within a procedure.

Syntax

 GoSublinelabel | linenumber}
. . .

linelabel: or linenumber
. . .

Return

Remarks

The GoSub...Return statement has these parts:

Part                        Description

 

GoSub                     Causes program flow to branch to a subroutine within a procedure.

linelabel                   Label that marks the first line of the subroutine.  The linelabel must begin with an alphabetic character, must end with a colon, must be 40 characters or less, and must not be a Visual Basic keywordJLVHLD.  Each linelabel must be unique in the module in which it is used.  Line labels are not case-sensitive and may begin in any column as long as they are the first nonblank characters on the line.

linenumber               Line number that marks the first line of the subroutine.  The linenumber can be any number of 40 characters or less, can contain only decimal digits (0-9), and must not end with a colon.  Each linenumber must be unique in the module in which it appears.  Line numbers18F50XF may begin in any column as long as they are the first nonblank characters.  If linenumber is greater than 65,529, the Erl function cannot properly report the line number where an error has occurred.

Return                     Causes program flow to return to the statement following the GoSub statement.  For every GoSub statement there must be an associated Return.

GoSub and Return can be used anywhere in a procedure, but the GoSub and corresponding Return must occur within the same procedure.  A subroutine can contain more than one Return statement, but the first Return statement encountered causes program flow to branch back to the statement immediately following the most recently executed GoSub statement.

 

Note   You cannot enter or exit Sub procedures with GoSub...Return.

 

 

Caution      Subroutines that call themselves (recursive subroutines) can easily run out of stack space and halt your program unexpectedly.

 


See Also

End StatementLANEND

GoTo StatementGU57HD

On...GoSub Statement, On...GoTo Statement5R9J0A9

Sub StatementLANSUB


GoSub...Return Statement Example

The example uses GoSub to call a subroutine from within a Sub procedure.  Notice that the GoTo branch avoids the subroutine.  This kind of branching is generally considered to be poor programming technique.  To avoid this kind of branching, turn subroutines into Sub procedures.  To try this example, paste the code into the Declarations section of a form.  Then press F5 and click the form.

 

Sub Form_Click ()

   Dim Num                         ' Declare variable.

   Num = InputBox("Type a number.")

   GoSub Routine                   ' Branch to subroutine.

   GoTo Nextpart                   ' Branch around subroutine.

Routine:                           ' Start of subroutine.

   Num = Num / 2

   Return                          ' Return from subroutine.

Nextpart:                          ' Continuation of program.

   Msgbox "Half of your number is " & Num

End Sub