GoSub Statement

Transfers program control to an internal subroutine. Not available in expressions.

Syntax

GoSub statement.label [ : ]

statement.label defines where the subroutine starts, and can be any valid label defined in the program.

: identifies the preceding text as a statement label to make the program more readable.

Remarks

You transfer control back to the main program using either a Return or Return To statement:

  • Return transfers program control to the statement following the GoSub statement.
  • Return To label transfers program control to a location in the program specified by label.

A program can call a subroutine any number of times. You can nest subroutines up to 256 deep.

Example

This example uses GoSub to call an internal subroutine within an IBM® InfoSphere® DataStage® transform function. The Return statement causes execution to resume at the statement immediately following the GoSub statement. It is necessary to use GoTo as shown to prevent control from accidentally flowing into the subroutine.

Function MyTransform(Arg1)
* Only use subroutine if input is a positive number:
   If Arg1 > 0 Then GoSub MyRoutine
   Reply = Arg1
   GoTo ExitFunction  ;* use GoTo to prevent an error
MyRoutine:
   Arg1 = SQRT(Arg1)  ;* take the square root
   Return             ;* return control to statement
ExitFunction:
Return(Reply)