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.

Syntaks:


GoSub label[:]

Parameterar:

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
Åtvaringsikon

Viss programmet kjem til eit RETURN-uttrykk utan tilhøyrande GoSub vil LibreOffice Basic gje ei feilmelding. Bruk Exit Sub eller Exit Function for å vera sikker på at programmet går ut av subrutinen eller funksjonen før det kjem til RETURN.


Eksempelet viser bruken av GOSUB og RETURN. Ved å køyra programmet to gongar, vil det rekna ut kvadratrota av to tal som brukaren skriv inn.

Eksempel:


Sub ExampleGoSub
Dim iInputa As Single
Dim iInputb As Single
Dim iInputc As Single
    iInputa = Int(InputBox$ "Skriv inn det første talet: ","Talinnskriving"))
    iInputb = Int(InputBox$ "Skriv inn det andre talet: ","Talinnskriving"))
    iInputc=iInputa
    GoSub SquareRoot
    Print "Kvadratrota av";iInputa;" er";iInputc
    iInputc=iInputb
    GoSub SquareRoot
    Print "Kvadratrota av";iInputb;" er";iInputc
    Exit Sub
SquareRoot:
    iInputc=sqr(iInputc)
    Return
End Sub