GOSUB

From Liberty BASIC Family
Jump to navigation Jump to search
Supported in Just BASIC Supported in Liberty BASIC Not supported in Liberty BASIC 5 Supported in Run BASIC

Description

GOSUB 'label' sends the program to the first statement after the label. The 'label' can be a branch label or a line number. The code is executed line by line until a RETURN statement is reached, whereupon, program control is transfered back to the first line following the GOSUB command. The code block between the 'label' and RETURN is called a subroutine.
See Branch Label.

Syntax

  1. gosub {[branchLabel]} or {line number}

Hints

This is an example of program flow "branching". The program execution branches to the subroutine indicated in the GOSUB command. When done, program control is sent back to the main program by the RETURN command to the line following the GOSUB statement.
See GOTO for an example of branching that does not return.
For more modern types of subroutines, see SUB and FUNCTION. They do have some advantages (local variables and parameter passing mechanism).

Example

' send program to a gosub routine
    gosub [myBranchLabel]

' back from gosub routine
    print "I'm back!"
    end

' the gosub label
[myBranchLabel]
'    ...do some stuff
    print "I'm in the subroutine"
' go back to line following gosub
    return

Useful Procedures

' Place a useful function or sub using this keyword here