beautypg.com

Sub, exit sub, end sub, End sub – Campbell Scientific CR9000X Measurement and Control System User Manual

Page 182

background image

Section 5. Program Declarations

SUB, EXIT SUB, END SUB

Declares the name, variables, and code that form a Subroutine.

Syntax

Sub

SubName [(VariableList )]

[

statementblock ]

[

Exit Sub ]

[

statementblock ]

End Sub

A Subroutine is a separate procedure that is called by the main program using a
Call statement. A Subroutine can take arguments, perform a series of
statements, and change the value of its arguments. However, a Subroutine can't
be used in an expression. You can call a Subroutine using the name followed
by the variable list.

See the Call topic in Section 9.1 Program Structure/ Control for specific
information on how to call Subroutines.

Subroutines must be declared before they are called in the program. The code
for a Subroutine cannot be contained within the code for another Subroutine;
however, a Subroutine can be called by another Subroutine. If one Subroutine
calls another, the second Subroutine must be placed in the code before the
Subroutine that calls it. Subroutines cannot be used in an expression.

Because of how data is buffered in the task sequencer, a subroutine call should
be the last item in the main body of the program. Measurement instructions
should never follow a call to a subroutine; doing so could result in bad data.

The Scan/NextScan instruction loop can be used within a Subroutine using a
different execution interval than the main program.

Variables declared by Dim within a subroutine or function are local to that
subroutine or function. The same variable name can be used within other
subroutines or functions or as a global variable without conflict. Variables used
as parameters to a subroutine or function are also local.

When a Subroutine is called from the Main Program Scan, a skipped scan will
occur if there is not sufficient time for the Subroutine
measurements/processing in addition to the main scan's
measurement/processing time requirements.

Caution

Subroutines can be recursive; that is, they can call themselves to perform a
given task. However, recursion can lead to strange results.

THE SUB STATEMENT HAS THESE PARTS:

Part Description

Sub

Marks the beginning of a

Subroutine

.

5-12