GOSUB

Since 3.1

GOSUB Label

Jump to a label declared elsewhere in the function. Then, if a RETURN instruction is encountered, the program comes back to run the code just after the GOSUB instruction.

GOSUB calls can be imbricated, until the memory is exhausted.

The same restrictions as GOTO apply

Computed GOSUB

Since 3.19

GOSUB Expression

Since Gambas 3.19, GOSUB (or GOTO) can take any expression as argument.

This expression must return an integer that represents the label you want to branch.

This integer is obtained by using labels as expression. In that case, a labels is transformed into an integer index representing it (only in the function where the label is defined).

Only labels that are outside of any control structure can be a target of a computed GOTO or GOSUB.

Example

Dim JumpArray As Integer[]

JumpArray = [LABEL_1, LABEL_2, LABEL_3]

GoSub JumpArray[Rand(1, 3)]
Return

LABEL_1:
  Print "One !"
  Return
LABEL_2:
  Print "Two !"
  Return
LABEL_3:
  Print "Three !"
  Return

See also