NAME

return - return from a subroutine

SYNOPSIS

return [LineNum]
return [LineLab]

DESCRIPTION

The return statement is used to stop executing a subroutine and return to the place from which the subroutine was called.  The return location is obtained by popping the topmost member of the stack of return locations which is set up by gosub.  Alternatively, a line number or label may be specified as the place to which to return.  In this case, the topmost member of the stack of return locations is popped and discarded without being used. 

Using more than one return in a subroutine allows you to return from different points in the subroutine. 

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. 

The program

	    gosub 100
	     .
	     .
	     .
	    rem
	100 rem	This is the subroutine
	    rem
	    if x > 0 then return 150
	    return
illustrates a return to some specific line. 

SEE ALSO

end, on gosub, gosub, stop, system

from The Basmark QuickBASIC Programmer’s Manual by Lawrence Leinweber