User Manual
MCP Series
Brushed DC Motor Controllers
MCP Series User Manual
162
GOSUB
Syntax
gosub label{[argument1,...,argumentN]}{,DataResult}
• Label - the go to label of the subroutine to be executed.
• Argument - is user dened arguments to send to the called subroutine. The only limit to the
amount of arguments used is program memory.
• DataResult - is an optional variable to store the value returned from called subroutine.
Description
The GOSUB command will jump to a specied label. After executing the code at the jump label a
RETURN command is then used to return the program to the next command after the last called
GOSUB.
There is no limit to this with Basic Micro Studio other than the size of the available stack
memory. GOSUB stores the address of the next command on the stack and jumps to the
specied label. User specied arguments can be dened in the subroutine. A return value
from the subroutine can be stored in the variable that is specied by the GOSUB DataResult
argument.
Notes
Subroutines must always exit via the RETURN command, which clears the saved address from
the stack and returns to the command following the calling GOSUB.
User dened arguments must match the number of arguments dened at the subroutine. If they
do not match, a stack overow or underow will happen.
If a subroutine returns a value, the GOSUB is not required to use it.
Example
The program below will print the results to the terminal window at 9600 baud. The results will be
110. The GOSUB command has two arguments and includes the DataResult variable. The values
10 and 100 are passed to the subroutine MyAdd. The values are then loaded into the variables
arg1 and arg2. Since RETURN can have an expression the variables arg1 and arg2 are added and
returned to the variable result.
Result var long
Main
Gosub myadd[10,100],result
puts 0,["Result =",dec result]
End
Arg1 var long
Arg2 var long
MyAdd [arg1,arg2]
Return arg1+arg2
See Also
RETURN