Defining a Subroutine
Navigation: PGMs ➔ Subroutines
|Defining a Function
|Defining a Subroutine
|Defining a Class
A subroutine is a set of common tasks that can be defined once and called multiple times from within a PGM. A subroutine is also a convenient way of grouping a set of tasks making code more legible.
A subroutine is similar to a function but does not return a result. So unlike a function it can not be included in an expression. A subroutine can be used instead of a function if a value does not need to be returned. If a value does need to be returned then a function will need to be used.
Sub subName (argument list)
subName : unique name of the function, used to refer to the function.
argument list : optional list of parameters in the following format: (datatype parameterName, datatype parameterName, etc.)
- The subroutine declaration must begin with the Sub keyword and end with the EndSub keyword.
- A subroutine may call itself (i.e. recursive subroutines are allowed). However, a true stack of the subroutine variables is not used.
- In contrast to a function, a Return statement cannot be used/is not required.
- The parameter list cannot include instance of classes or variables passed by reference.
- Empty parentheses must be inserted, if no parameters are required by the subroutine.
- Local variables can be declared within the subroutine, and their scope is limited to the subroutine. However when a subroutine calls itself recursively a local stack is not used.
- Subroutine definitions cannot be nested (i.e. subroutines cannot be defined within subroutines).
Examples and Use
Sub CapacityCheck(Str unit, real capacity)
Feed_tag = Concatenate(unit, "QFeed.Qm (t/h)")
If ([Feed_tag] > capacity)
Lognote(Concatenate(unit, " has exceeded its capacity"))
;check capacity of unit using subroutine
See also: Example PGM Files