GOSUB/GOSUB DWORD statements

Purpose

Invoke a subroutine.

Syntax

GOSUB {label | linenumber}

GOSUB DWORD dwpointer

Remarks

GOSUB causes execution to branch to the statement prefaced by label or linenumber, after first saving its current location on the stack.  The label or linenumber must be local to the Sub or Function where the GOSUB statement is located.  Executing a RETURN statement returns control to the instruction immediately following the GOSUB.

When using GOSUB, be sure that each subroutine returns to its caller gracefully through a RETURN statement.  Run-away (recursive) GOSUBs that loop upon themselves will eat up large chunks of stack space, reducing available memory.

All labels and line numbers are private.  You cannot GOSUB to a label outside of the current Sub or Function.

For time critical or high-performance code, using a GOSUB to perform a repetitive task is almost always faster then performing a call to a Sub or Function, since there is no overhead in setting up a stack frame for a GOSUB.

DWORD

GOSUB DWORD causes execution to branch to address stored in dwpointer, after first saving its current location on the stack.  dwpointer must be a Double word, Long integer, or Pointer variable that contains the address of a label that is in the same Sub or Function as the GOSUB DWORD statement.  Executing a RETURN statement returns control to the instruction immediately following the GOSUB DWORD statement.

See also

#STACK, FUNCTION/END  FUNCTION, ON GOSUB, SUB/END SUB, RETURN

Example

FUNCTION DoCalc!(Radius!)

  pi# = ATN(1) * 4    ' calculate value of PI

  GOSUB CalcArea      ' jump to subroutine Radius!

  EXIT FUNCTION

 

CalcArea:

  FUNCTION = pi# * Radius! ^2  ' calculate area

  RETURN            ' return from subroutine

END FUNCTION