Defining a Subroutine
Navigation: PGM SITEMAP
|Defining a Class||Defining a Function||Mathematical Functions||Defining a Subroutine||Tag Functions|
|Predefined Classes||Predefined Functions||String Functions||Trigger Subroutines||Example PGM Files|
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) ...statements... EndSub
subName : is an 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 (ie 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 (ie subroutines cannot be defined within subroutines).
Examples and Use
Sub CapacityCheck(Str unit, real capacity) Str Feed_tag Feed_tag = Concatenate(unit, "QFeed.Qm (t/h)") If ([Feed_tag] > capacity) Lognote(Concatenate(unit, " has exceeded its capacity")) Endif EndSub
;check capacity of unit using subroutine CapacityCheck("Spiral_1", 20)