Subprocedures and Subroutines
Subprocedures and Subroutines
A subprocedure is similar to a subroutine, except that a subprocedure offers the following improvements:
-
You can pass parameters to a subprocedure, even passing by value.
This means that the parameters used to communicate with subprocedures do not have to be modifiable. Parameters that are passed by reference, as they are with programs, must be modifiable, and so may be less reliable.
-
The parameters passed to a subprocedure and those received by it are checked at compile time for consistency. This helps to reduce run-time errors, which can be more costly.
-
You can use a subprocedure like a built-in function in an expression.
When used in this way, they return a value to the caller. This basically allows you to custom-define any operators you might need in an expression.
-
Names defined in a subprocedure are not visible outside the subprocedure.
This means that there is less chance of the procedure inadvertently changing a item that is shared by other procedures. Furthermore, the caller of the procedure does not need to know as much about the items used inside the subprocedure.
-
You can call the subprocedure from outside the module, if it is exported.
-
You can call subprocedures recursively.
-
Procedures are defined on a different specification type, namely, procedure specifications. This different type helps you to immediately recognize that you are dealing with a separate unit.
If you do not require the improvements offered by subprocedures, you may want to use a subroutine because an EXSR operation is usually faster than a call to a subprocedure.