PushVariables and PopVariables Commands

By default, subroutines called with GoSub commands treat all variables as global variables. This style might cause conflicts between variables used in subroutines and variables used in the main code and it also precludes coding options that call subroutines recursively. To change this default behavior, you can call subroutines using:

PushVariables
GoSub subname #1,#2,...#n
PopVariables

The PushVariables command saves a copy of all current variables and the PopVariables command returns them to previous values after the subroutine is done. The subroutine will have access to all variables in the main commands, but any changes made to those variables will be restored to their initial values after the PopVariables command. Similarly, any new variables defined in the subroutine will be returned to undefined variables after the PopVariables command.

Because variables are reset after the PopVariables command, the only way for a subroutine call bracketed by PushVariables and PopVariables to get a variable back to the main commands is to exit the subroutine using:

return (expr)

where (expr) is any expression that may include current variables in the subroutine. The calling code can retrieve this result using the #returnValue$ read-only variable. This read-only variable will remain valid even after the PopVariables command.

Notes

  1. All alternative to pushing and popping all variables is to use Local commands to define only selected variables as local to a subroutine (Local commands are also more efficient because they do not need to push and pop all currently defined variables).
  2. PushVariables and PopVariables groupings can be nested when using nested subroutine calls. This feature allows code to make use of recursive subroutines.
  3. When writing internal scripts, object names are always global even when using PushVariables and PopVariables commands. Subroutines should be careful not to change object names in the main commands. Because they are global, object names defined in the subroutine can be read in the main commands without needed the return commmand.