Subroutines
A subroutine is a user-defined command that can be called to perform a block of logic, and optionally to return a value. Subroutines are normally used when the same set of statements is repeated in multiple places, such as at multiple activities. In this case, calling the same subroutine from each lcoation simplifies the logic and provides a single source where the logic can be modified. A subroutine may be named any unique, valid name.
A subroutine may have parameters that act as local variables inside the subroutine and that take the values of the arguments (i.e., numeric expressions) that are passed to the subroutine. Additionally, a subroutine can return either an integer value or a real value to the logic or property field that called the subroutine.
To access information about the Subroutines in your model, go to Model Elements > Subroutines.
Defining Subroutines
The following Model Elements image shows three defined subroutines. Subroutine1 does not return any value and does not have any parameters. Subroutine2 returns an integer value when it has completed its logic, and it does not have any parameters. Subroutine3 returns a real value when it has completed its logic, and it is defined to have two parameters (Parameter1 and Parameter2) that receive values from two corresponding arguments passed to the subroutine when it is called.
The following Subroutines field descriptions are divided into a grid listing the subroutine names, along with a Logic window and toolbar that updates according to the subroutine selected.
- Name — A descriptive label to identify the subroutine and its intended purpose. You can double-click the name to rename it. A subroutine may be named any unique, valid name. This name is used in the logic to call the subroutine.
- Logic Window — Can contain one or more executable statements whenever the subroutine is called. Statements in subroutines must be valid in the logic that calls the subroutine. Subroutine logic may contain a Return statement with a value to be returned from the subroutine (refer to Return for the correct statement syntax). As you select a listed subroutine, the Logic window (on the right) updates to show logic specific to that subroutine. The Logic toolbar also updates and shows subroutine-specific details for any Parameters or Return Type.
The Logic window also has the following toolbar:
The buttons (from left to right) are the Statement list, Cut, Copy, Paste, Outdent, Indent, Comment, Uncomment, Parameters, and Return Type.
- Parameters — Parameters are the argument values passed to the subroutine and get assigned to local variables within the Subroutine Logic. Parameters can be defined as real or integer types. The first parameter receives the first argument, the second parameter receives the second argument, and so on. Select the Logic Toolbar Parameters icon and select the Subroutine Parameters Add to type the name and select the data type (Integer or Real).
- Return Type — This defines the numeric value form returned by the subroutine, which can be real, integer, or none. Select the Logic Toolbar Return Type icon and select Real for the subroutine to return a real type number and Integer for the subroutine to return an integer type. Select None when no return value is expected.
Using Subroutines
ProModel AutoCAD® Edition handles subroutines in two ways. First and most commonly, a subroutine may be processed by the calling logic as though the subroutine is part of the calling logic. When a subroutine in encountered in logic, ProModel executes the statements contained in the subroutine directly inline with the logic from where it is called. All logic before a subroutine is executed before the subroutine's logic. All logic after a subroutine is executed after the subroutine's logic has finished. You can reference the subroutine's name in some logic or expression to execute.
Second, a subroutine may be processed independently of the calling logic so the calling logic continues without waiting for the subroutine to finish. This method requires an Activate statement followed by the subroutine name.
Generally, you can call a subroutine without arguments using the following format:
SubroutineName()
Use the following format when calling a subroutine with arguments:
SubroutineName(arg1,arg2, ...,argn)
Where arg1 is the first argument name, arg2 is the second argument name, and argn is the nth argument name.
Miscellaneous Information about Calling and Using Subroutines
- Subroutine with no arguments must still have opening and closing parentheses in the calling statement.
- Statements in subroutines must be valid in the logic that called the subroutine. For example, if a subroutine is called from location logic, the subroutine may contain only those statements valid in the location logic. Subroutines called from an Activate statement can have any general logic statements, including Wait.
- A subroutine may be used in any logic. Additionally, a subroutine may be used in most property fields, provided the Return statement is used in the subroutine to return an appropriate value to the property field.
- If a subroutine is defined to return either a real or integer value, but does not use a Return statement to return a value, a 0 (zero) value is returned by default. No value is returned for a None subroutine type.
- When using the Activate statement to call a subroutine, the calling logic continues without waiting for the called subroutine to finish. If a return value is defined, it is ignored by the calling logic. Therefore, activated subroutines can run in parallel with the logic that called them. Refer to Activate for additional information.
- Activated subroutines cannot contain entity-specific or location-specific system functions.
The following examples are calls to subroutines:
- Elapsed_Time()
- Calculate_Sums(Set1, Set2, Set3)
- Average = Compute_Average(Expenses)
- Activate Monitor_Levels()