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

Parameters:

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
警告圖示

當 LibreOffice Basic 發現 Return 陳述式前面沒有 GoSub 陳述式時,將傳回錯誤訊息。使用 Exit SubExit Function 以確保程式在執行到下一個 Return 陳述式之前結束子常式或函式。


以下示例用於說明 GoSubReturn 的用法。透過將程式段執行兩遍,計算由使用者輸入的兩個數的平方根。

Example:


Sub ExampleGoSub
Dim iInputa As Single
Dim iInputb As Single
Dim iInputc As Single
    iInputa = Int(InputBox("Enter the first number:","NumberInput"))
    iInputb = Int(InputBox("Enter the second number:","NumberInput"))
    iInputc=iInputa
    GoSub SquareRoot
    Print "The square root of";iInputa;" is";iInputc
    iInputc=iInputb
    GoSub SquareRoot
    Print "The square root of";iInputb;" is";iInputc
    Exit Sub
SquareRoot:
    iInputc=sqr(iInputc)
    Return
End Sub

Please support us!