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, LibreOffice 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("Введіть перше число: ","Введення числа"))
    iInputb = Int(InputBox("Введіть друге число: ","Введення числа"))
    iInputc=iInputa
    GoSub SquareRoot
    Print "Квадратний корінь із";iInputa;" рівний";iInputc
    iInputc=iInputb
    GoSub SquareRoot
    Print "Квадратний корінь із";iInputb;" рівний";iInputc
    Exit Sub
SquareRoot:
    iInputc=sqr(iInputc)
    Return
End Sub

Будь ласка, підтримайте нас!