LibreOffice 7.3 Help
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
' instructies
GoSub label
' instructies
Exit Sub/Function
label:
' instructies
Return
End Sub/Function
Als het programma een Return-instructie tegenkomt die niet door GoSub voorafgegaan wordt, geeft LibreOffice Basic een foutmelding. Gebruik Exit Sub of Exit Function om ervoor te zorgen dat het programma een 'Sub' of 'Function' verlaat voordat de volgende Return-instructie bereikt wordt.
In het volgende voorbeeld wordt het gebruik van GoSub en Return uitgelegd. Het programma kan de vierkantswortel berekenen van twee getallen die door de gebruiker worden ingevoerd, door een programmasectie tweemaal uit te voeren.
Sub ExampleGoSub
Dim iInputa As Single
Dim iInputb As Single
Dim iInputc As Single
iInvoera = Int(InputBox$ ("Voer het eerste getal in: ","Getalinvoer"))
iInvoerb = Int(InputBox$ ("Voer het tweede getal in: ","Getalinvoer"))
iInputc=iInputa
GoSub SquareRoot
Print "De vierkantswortel van ";iInvoera;" is";iInvoerc
iInputc=iInputb
GoSub SquareRoot
Print "De vierkantswortel van";iInvoerb;" is";iInvoerc
Exit Sub
SquareRoot:
iInputc=sqr(iInputc)
Return
End Sub