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.

语法:


GoSub label[:]

参数:

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
警告图标

如果程序遇到 Return 语句之前没有「GoSub」,Collabora Office Basic 会返回一个错误信息。使用「Exit Sub」或「Exit Function」能够保证程序在到达下一个 Return 语句之前离开子程序或者函数。


以下示例用于说明「GoSub」和「Return」的使用。通过将程序段执行两遍,计算由用户输入的两个数的平方根。

示例:


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

请支持我们!