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.
GoSub label[:]
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
Trifft das Programm auf eine Return-Anweisung, ohne dass zuvor ein GoSub erfolgte, meldet LibreOffice Basic eine Fehlermeldung. Sie müssen selber dafür Sorge tragen, dass Ihr Programm ein Unterprogramm oder eine Funktion mit der Anweisung Exit Sub beziehungsweise der Anweisung Exit Function (siehe dort) verlässt, bevor es auf einen Programmteil stößt, der mit Return abgeschlossen ist.
Das folgende Beispiel demonstriert die Verwendung von GoSub und Return. Das Programm berechnet die Quadratwurzeln zweier vom Benutzer eingegebener Zahlen, indem es einen bestimmten Programmabschnitt zweimal ausführt.
Sub ExampleGoSub
Dim iInputa As Single
Dim iInputb As Single
Dim iInputc As Single
iInputa = Int(InputBox("Bitte geben Sie die erste Zahl ein: ","Zahleingabe"))
iInputb = Int(InputBox("Bitte geben Sie die zweite Zahl ein: ","Zahleingabe"))
iInputc=iInputa
GoSub SquareRoot
Print "Die Quadratwurzel aus";iInputa;" ist";iInputc
iInputc=iInputb
GoSub SquareRoot
Print "Die Quadratwurzel aus";iInputb;" ist";iInputc
Exit Sub
SquareRoot:
iInputc=sqr(iInputc)
Return
End Sub