GoSub...Return Statement
Calls a subroutine that is indicated by a label from a subroutine 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 \<emph\>GoSub \</emph\>statement.
Syntax:
see Parameters
Parameters:
Sub/Function
statement block
Label
statement block
GoSub Label
Exit Sub/Function
Label:
statement block
Return
End Sub/Function
The \<emph\>GoSub\</emph\> 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 (":").
If the program encounters a Return statement not preceded by \<emph\>GoSub\</emph\>, LibreOffice Basic returns an error message. Use \<emph\>Exit Sub\</emph\> or \<emph\>Exit Function\</emph\> to ensure that the program leaves a Sub or Function before reaching the next Return statement.
The following example demonstrates the use of \<emph\>GoSub\</emph\> and \<emph\>Return\</emph\>. By executing a program section twice, the program calculates the square root of two numbers that are entered by the user.
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