SUB xxx (arg1 [,arg2, …])
Defines a callable subroutine. This is the same as adding a new command to MMBasic while it is running your program.
‘xxx’ is the subroutine name and it must meet the specifications for naming a variable. ‘arg1’, ‘arg2’, etc are the arguments or parameters to the subroutine.
Every definition must have one END SUB statement. When this is reached the program will return to the next statement after the call to the subroutine. The command EXIT SUB can be used for an early exit.
You use the subroutine by using its name and arguments in a program just as you would a normal command.
MySub a1, a2
When the subroutine is called each argument in the caller is matched to the argument in the subroutine definition. These arguments are available only inside the subroutine. Subroutines can be called with a variable number of arguments. Any omitted arguments in the subroutine’s list will be set to zero or a null string.
Arguments in the caller’s list that are a variable (ie, not an expression or constant) will be passed by reference to the subroutine. This means that any changes to the corresponding argument in the subroutine will also be copied to the caller’s variable and therefore may be accessed after the subroutine has ended.
Brackets around the argument list in both the caller and the definition are optional.