Subroutines are syntactically similar to modules that are launched implicitly by being called inside of a script, with the main difference being that subroutine modules have one or more Return statements in them. Their behavior is somewhat different, however, since the calling module will suspend execution of the script that started the subroutine until the subroutine executes the Return. Once the Return is executed, the subroutine module is stopped immediately and the calling script resumes. This allows the building of modules that return a value that can be used for subsequent statements in a script.
A module that has a Return statement in it is only considered a subroutine, and will only behave in the manner as described above, if it is called inside of a script. Program execution will not be suspended if the module containing the Return is called outside of a script.
The only other difference between subroutines and launched modules is how they launch other modules; as previously described in the section on launched modules, a module launched by a subroutine will not take the subroutine as its parent and/or caller, but rather, will consider the nearest non-subroutine parent as its parent and caller.
WARNING: Great care must be taken in the use of subroutines, since no other statements, I/O or alarms will be executed while a subroutine is running. This means that if a sub-routine launches a module with the intention of waiting for the results of its execution, the application will hang, since even child modules will be blocked by their calling sub-routine. It is vital to ensure that a subroutine executes a Return statement since all other module will be suspended until this occurs.
Topics in this section: