Defining a Subroutine

From SysCAD Documentation
Jump to navigation Jump to search

Navigation: PGMs ➔ Subroutines

Functions Subroutines Classes
Defining a Function Predefined Functions Tag Functions Mathematical Functions String Functions Defining a Subroutine Trigger Subroutines Defining a Class

Introduction

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.

Syntax

Sub subName(argument list)
   ...statements...
EndSub

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.
  • In contrast to a function, a Return statement is not required, and cannot be used.
  • 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.
  • A subroutine may call itself (i.e. recursive subroutines are allowed). However, a true stack of the subroutine variables is not used.
  • 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.
  • Any Const variable declared in a subroutine is added to the global scope and is accessible outside of the subroutine. A constant is not limited to the scope of the subroutine.
  • Subroutine definitions cannot be nested (i.e. subroutines cannot be defined within subroutines).

Examples and Use

Subroutine Examples

Example 1

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

Using Subroutines...

;check capacity of unit using subroutine
CapacityCheck("Spiral_1", 20)

See also: Example PGM Files