GoSub...Return lause

Sisukord
Register 🔎︎

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

### Süntaks:

    GoSub label[:]

### Parameetrid:

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
          ' laused
          GoSub label
          ' laused
          Exit Sub/Function
      label:
          ' laused
          Return
      End Sub/Function

Kui programm jõuab Return-lauseni, millele ei eelne lauset GoSub, siis tagastab LibreOfficeDev Basic veateate. Selleks, et programm sulgeks enne järgmise Return-lauseni jõudmist alamprotseduuri või funktsiooni, kasuta lauset Exit Sub või Exit Function.

Järgmine näide on lausete GoSub ja Return kasutamise kohta. Programmiosa kaks korda käivitamisel arvutab programm kasutaja sisestatud kahe arvu ruutjuure.

### Näide:

    Sub ExampleGoSub
    Dim iInputa As Single
    Dim iInputb As Single
    Dim iInputc As Single
        iInputa = Int(InputBox("Sisesta esimene arv: ","ArvuSisestus"))
        iInputb = Int(InputBox("Sisesta teine arv: ","ArvuSisestus"))
        iInputc=iInputa
        GoSub SquareRoot
        Print "Ruutjuur arvust";iInputa;" on";iInputc
        iInputc=iInputb
        GoSub SquareRoot
        Print "Ruutjuur arvust";iInputb;" on";iInputc
        Exit Sub
    SquareRoot:
        iInputc=sqr(iInputc)
        Return
    End Sub

Sarnased teemad

GoTo lause

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