GoSub...Return Statement

Calls a subroutine that is indicated by a label inside a Sub or a Function. The statements following the label are executed until the next Return statement. Afterwards, the program continues with the statement that follows the GoSub statement.

Syntaxe:


GoSub label[:]

Parametry:

label: A line identifier indicating where to continue execution. The scope of a label in that of the routine it belongs to.

The GoSub statement calls a local subroutine indicated by a label from within a subroutine or a function. The name of the label must end with a colon (":").


  Sub/Function foo
      ' statements
      GoSub label
      ' statements
      Exit Sub/Function
  label:
      ' statements
      Return
  End Sub/Function
Ikona upozornění

Pokud program narazí na příkaz Return, kterému nepředchází GoSub, LibreOffice Basic vrátí chybovou zprávu. Pomocí Exit Sub nebo Exit Function zajistíte, že program opustí Sub nebo Function před dosažením příkazu Return.


Následující příklad demonstruje použití GoSub a Return. Dvojitým spuštěním části programu se vypočte odmocnina dvou zadaných čísel.

Příklad:


Sub ExampleGoSub
Dim iInputa As Single
Dim iInputb As Single
Dim iInputc As Single
    iInputa = Int(InputBox("Zadejte první číslo: ","Číselný vstup"))
    iInputb = Int(InputBox("Zadejte druhé číslo: ","Číselný vstup"))
    iInputc=iInputa
    GoSub SquareRoot
    Print "Odmocnina z";iInputa;" je";iInputc
    iInputc=iInputb
    GoSub SquareRoot
    Print "Odmocnina z";iInputb;" je";iInputc
    Exit Sub
SquareRoot:
    iInputc=sqr(iInputc)
    Return
End Sub