QBzdW‖

cfNGR‖GoSub...Return Statement

HSYep‖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.

FVEx2‖Syntax:


g6Wgg‖GoSub label[:]

WADQ4‖Parameters:

krBDs‖label: A line identifier indicating where to continue execution. The scope of a label in that of the routine it belongs to.

sqKLC‖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
  Fvi98‖    ' statements
      GoSub label
  ntDMU‖    ' statements 
      Exit Sub/Function
  label:
  9TJLT‖    ' statements 
      Return
  End Sub/Function
Warning Icon

X2mAh‖If the program encounters a Return statement not preceded by GoSub, LibreOfficeDev Basic returns an error message. Use Exit Sub or Exit Function to ensure that the program leaves a Sub or Function before reaching the next Return statement.


HZDqC‖The following example demonstrates the use of GoSub and Return. By executing a program section twice, the program calculates the square root of two numbers that are entered by the user.

EFSA4‖Example:


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