NAME

gosub - branch to a subroutine

SYNOPSIS

gosub LineNum
gosub LineLab

DESCRIPTION

Execution of the gosub statement causes a transfer to the specified line number LineNum or line label LineLab.  Prior to the transfer, the location of the next statement in sequence is saved on a stack of return locations. 

Subroutines may be nested, i.e. a subroutine may be called from within another subroutine.  The only limit on the nesting of subroutines is the availability of stack space for the return locations.  A subroutine call may not cause control to be transferred into or out of a subprogram or user defined function. 

Using a stop, end, or goto statement before a subroutine allows program control to be directed around it.  For example, you may want to prevent a program from entering the subroutine accidentally. 

In order to branch to different subroutines depending on the result of an expression, use on gosub

EXAMPLE

The program
	    rem
	    rem	Program to illustrate nested subroutine
	    rem		calls and returns
	    rem
	 10 x = 1 : y = 2	' initialize x and y
	 20 gosub 100		' swap the values of x and y
	 30 x = 8 : y = 42	' initialize x and y again
	 40 gosub 100		' swap the values of x and y
	 50 end			' explicit END prevents fall-through
	    rem
	    rem	Subroutine for swapping values of x and y
	    rem
	100 gosub 200		' print the values of x and y
	110 s = x : x = y : y = s	' do the swap
	120 gosub 200		' print the values of x and y
	130 return
	    rem
	    rem	Subroutine for printing values of x and y
	    rem
	200 print "X is"; x; ", Y is"; y
	210 return
produces
	X is 1 , Y is 2
	X is 2 , Y is 1
	X is 8 , Y is 42
	X is 42 , Y is 8
After the variables x and y are initialized in line 10, the gosub in line 20 calls the swapping subroutine located at line 100.  The return location of line 30 is saved on the stack of return locations and control branches to line 100 where execution of the swapping subroutine begins. 

From the swapping subroutine, the printing subroutine is called from the gosub statement at line 100.  The return location of line 110 is saved on the stack of return locations and control branches to line 200 where the printing is done.  The return statement at line 210 causes the topmost element of the stack of return locations (in this case, line 110) to be popped and used as the place to which to return control.  At line 110, the swap is performed.  At line 120, the printing subroutine is called again.  The return location of line 130 is saved on the stack of return locations and control again transfers to line 200 where the printing is done.  The return at line 210 caused the topmost element of the stack of return locations (now, line 130) to be popped and used as the place to which to return control.  At line 130, another return statement is encountered, so the topmost element of the stack of return locations (line 30) is popped and used as the place to which to return. 

At line 30, x and y are given new values, and at line 40 the swapping subroutine is called again.  The execution of this call to the swapping subroutine proceeds similarly to the first call at line 10.  Eventually, control returns from the swapping subroutine to line 40.  The end statement at line 40 is necessary to prevent execution from "falling through" to the swapping subroutine. 

SEE ALSO

goto, on gosub, on goto, return

from The Basmark QuickBASIC Programmerís Manual by Lawrence Leinweber