beautypg.com

Campbell Scientific CR5000 Measurement and Control Module User Manual

Page 92

background image

Section 5. Program Declarations

5-4

The Sub statement has these parts:

Part

Description

Sub

Marks the beginning of a Subroutine.

SubName

Name of the Subroutine. Because Subroutine names are
recognized by all procedures in all modules, subname cannot
be the same as any other globally recognized name in the
program.

VariableList

List of variables that are passed to the Subroutine when it is
called. The variable names used in this list should not be the
same names as variables, aliases, or constants declared
elsewhere. The variable names in this list can only be used
within the Subroutine. Multiple variables are separated by
commas. When the Subroutine is called, the call statement
must list the program variables or values to pass into the
subroutine variable. The number and sequence of the
program variables/values in the call statement must match the
number and sequence of the variable list in the sub
declaration. Changing the value of one of the variables in this
list inside the Subroutine changes the value of the variable
passed into it in the calling procedure.

The call may pass constants or expressions that evaluate to
constants (i.e., do not contain a variable) into some of the
variables. If a constant is passed, the “ variable” it is passed
to becomes a constant and cannot be changed by the
subroutine. If constants will be passed, the subroutine should
be written to not try to change the value of the “ variables”
they will be passed into.

statementblock

Any group of statements that are executed within the body of
the Subroutine.

Exit Sub

Causes an immediate exit from a Subroutine. Program
execution continues with the statement following the
statement that called the Subroutine. Any number of Exit
Sub
statements can appear anywhere in a Subroutine.

End Sub

Marks the end of a Subroutine.

A Subroutine is a procedure that can take variables, perform a series of statements,
and change the value of the variables. 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 statement for specific information on how to call Subroutines.

The list of Subroutine variables to pass is optional. Subroutines can operate on the
global program variables declared by the Public or Dim statements. The advantage
of passing variables is that the subroutine can be used to operate on whatever
program variable is passed (see example).

Caution

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