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[:]

Parametre:

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
Advarselsikon

Hvis programmet møder en Return-sætning, der ikke er indledt med GoSub, returnerer LibreOffice Basic en fejlmeddelelse. Brug Exit Sub eller Exit Function for at sikre, at programmet forlader en Sub eller en Function, før det når næste Return-sætning.


Følgende eksempel demonstrerer brugen af GoSub og Return. Ved at udføre en programsektion to gange beregner programmet kvadratroden af to tal, som er indtastet af brugeren.

Eksempel:


Sub ExampleGoSub
Dim iInputa As Single
Dim iInputb As Single
Dim iInputc As Single
    iInputa = Int(InputBox$("Indtast det første tal: ","Talindtastning"))
    iInputb = Int(InputBox$("Indtast det andet tal: ","Talindtastning"))
    iInputc=iInputa
    GoSub SquareRoot
    Print "Kvadratroden af";iInputa;" er";iInputc
    iInputc=iInputb
    GoSub SquareRoot
    Print "Kvadratroden af";iInputb;" er";iInputc
    Exit Sub
SquareRoot:
    iInputc=sqr(iInputc)
    Return
End Sub