Pomoc LibreOffice 7.5
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
Jeśli program napotka instrukcję Return, która nie była poprzedzona instrukcją GoSub, LibreOffice zwraca komunikat o błędzie. W celu upewnienia się, że program opuści procedurę (Sub) lub funkcję (Function), zanim napotka następną instrukcję Return, należy użyć instrukcji Exit Sub lub Exit Function.
Poniższy przykład przedstawia użycie instrukcji GoSub i Return. Dwukrotne uruchomienie sekcji programu powoduje obliczenie pierwiastka kwadratowego dwóch liczb wprowadzonych przez użytkownika.
Sub ExampleGoSub
Dim iInputa As Single
Dim iInputb As Single
Dim iInputc As Single
iInputa = Int(InputBox("Wprowadź pierwszą liczbę: ","Wprowadzenie liczby"))
iInputb = Int(InputBox("Wprowadź drugą liczbę: ","Wprowadzenie liczby"))
iInputc=iInputa
GoSub SquareRoot
Print "Pierwiastek kwadratowy z ";iInputa;" wynosi ";iInputc
iInputc=iInputb
GoSub SquareRoot
Print "Pierwiastek kwadratowy z ";iInputb;" wynosi ";iInputc
Exit Sub
SquareRoot:
iInputc=sqr(iInputc)
Return
End Sub