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
Biểu tượng Cảnh báo

Nếu chương trình gặp một câu lệnh Return không có GoSub đi trước, LibreOffice Basic sẽ trả về một thông điệp lỗi. Hãy dùng Exit Sub hay Exit Function để đảm bảo chương trình thoát khỏi một chương trình con hay hàm trước khi tới câu lệnh Return kế tiếp.


Mẫu ví dụ theo đây minh hoạ sử dụng hai câu lệnh GoSubReturn. Bằng cách thực hiện một phần chương trình hai lần, chương trình tính căn bậc hai của hai số được người dùng nhập.

Example:


Sub ExampleGoSub
Dim iInputa As Single
Dim iInputb As Single
Dim iInputc As Single
    iInputa = Int(InputBox("Gõ số thứ nhất: ","NumberInput"))
    iInputb = Int(InputBox("Gõ số thứ hai: ","NumberInput"))
    iInputc=iInputa
    GoSub SquareRoot
    Print "Căn bậc hai của";iInputa;" là";iInputc
    iInputc=iInputb
    GoSub SquareRoot
    Print "Căn bậc hai của";iInputb;" là";iInputc
    Exit Sub
SquareRoot:
    iInputc=sqr(iInputc)
    Return
End Sub

Please support us!