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.

Syntax:


GoSub label[:]

Parametrar:

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
Varningssymbol

Om programmet pÄtrÀffar ett Return-uttryck som inte föregÄs av GoSub, returnerar LibreOffice Basic ett felmeddelande. AnvÀnd Exit Sub eller Exit Function om du vill se till att programmet avslutar en subrutin eller funktion innan nÀsta Return-uttryck aktiveras.


NedanstÄende exempel visar hur GoSub och Return anvÀnds. Genom att köra ett programavsnitt tvÄ gÄnger berÀknar programmet kvadratroten ur tvÄ tal som anvÀndaren anger.

Exempel:


Sub ExampleGoSub
Dim iInputa As Single
Dim iInputb As Single
Dim iInputc As Single
    iInputa = Int(InputBox("Ange det första talet: ","Nummerinmatning"))
    iInputb = Int(InputBox("Ange det andra talet: ","Nummerinmatning"))
    iInputc=iInputa
    GoSub SquareRoot
    Print "Kvadratroten ur";iInputa;" Àr";iInputc
    iInputc=iInputb
    GoSub SquareRoot
    Print "Kvadratroten ur";iInputb;" Àr";iInputc
    Exit Sub
SquareRoot:
    iInputc=sqr(iInputc)
    Return
End Sub

Please support us!