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 GoSub statement.
Sintaxe:
ver Parámetros
Parámetros:
Sub/Function
bloque de instrucións
Etiqueta
bloque de instrucións
Etiqueta GoSub
Exit Sub/Function
Etiqueta:
bloque de instrucións
Volver
End Sub/Function
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 (":").
If the program encounters a Return statement not preceded by GoSub, LibreOffice 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.
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.
Exemplo:
Sub ExampleGoSub
Dim iInputa As Single
Dim iInputb As Single
Dim iInputc As Single
iInputa = Int(InputBox("Introduza o primeiro número: ","EntradaNúmero"))
iInputb = Int(InputBox("Introduza o segundo número: ","EntradaNúmero"))
iInputc=iInputa
GoSub SquareRoot
Print "A raíz cadrada de";iInputa;" é";iInputc
iInputc=iInputb
GoSub SquareRoot
Print "A raíz cadrada de";iInputb;" de";iInputc
Exit Sub
SquareRoot:
iInputc=sqr(iInputc)
Return
End Sub