GOSUB Statement

Previous  Next

 

GOSUB label

 


 

The MyBASIC2 command GOSUB jumps to a subroutine at the indicated label. This call is placed onto the stack. The subroutine finalizes using a RETURN command. Program execution continues at the command following the initial GOSUB command.

 

Example:

 

' This script is an example of MyBASIC2

BEGIN:

  n = 10

  DIM arr(10) AS REAL

  GOSUB CALC

  GOSUB SHOW

END

 

CALC:

  arr(0) = 1

  FOR i = 1 TO n - 1

     IF i = 1 THEN arr(i) = 1 ELSE arr(i) = arr(i - 1) + arr(i - 2)

  NEXT

RETURN

 

SHOW:

  FOR i = 0 TO n - 1

     PRINT arr(i); ", "

  NEXT

  PRINT ,

RETURN