Contents
Index 🔎︎

# 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.

### Syntax:

    GoSub label[:]

### Parameters:

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은 오류 메시지를 반환합니다. 다음 Return 문에 도달하기 전에 Sub 또는 Function을 끝내려면 Exit Sub 또는 Exit Function을 사용합니다.

다음은 GoSub 및 Return 사용을 보여 주는 예입니다. 프로그램 구역을 두 번 실행함으로써 프로그램은 사용자가 입력한 두 숫자의 제곱근을 계산합니다.

### 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

관련 주제

GoTo Statement

On...GoSub Statement; On...GoTo Statement