Subroutines

<< Click to Display Table of Contents >>

Navigation:  Reference > Pre-processor >

Subroutines

PP subroutines are defined with a [*SBDEF] command and end with a [*SBEND] command. To use a subroutine a [*SBCALL] command is used. Any number of calls can be made to a subroutine. Subroutines can be called within a subroutine. Up to 100 indices can be passed as parameters to a subroutine.

A simple subroutine without parameters could be defined as follows:

[*sbdef snRepeat]

[*set RepeatCalls = RepeatCalls + 1]

! this subroutine has been called [RepeatCalls] times

[*sbend snRepeat]

[*set RepeatCalls = 0]

[*sbcall snRepeat]

[*sbcall snRepeat]

IMPORTANT: a subroutine must be defined before it is called.

The code within a subroutine is not parsed until it is called. In the above example RepeatCalls can be used in the subroutine even though it is not set until after the definition.

A subroutine call can pass over indices as parameters:

[*sbdef snShow=SHOWspWhich]

! This is [SHOWspWhich]

[*sbend snShow]

[*set Wanted =56]

[*sbcall snShow=Wanted]

[*set Wanted =’this one’]

[*sbcall snShow=Wanted]

will output two lines:

! This is 56

! This is this one

Up to 100 parameters can be passed separated by commas. Whenever the subroutine refers to a parameter it is the index in the same position in the list of the *SBCALL that is actually referred to. Any type of index can be passed to a subroutine including data sets and ODBC files and loop indices. Any changes made to the values of parameters will change the passed index value.

[*sbdef snReturn=RETspdsDataSet,RETspWhich,RETspValue]

[*set RETspValue=[RETspdsDataSet.RETspWhich]]

[*sbend snReturn]

[*data dsMyList=1,5,7]

[*set MyValue=0]

[*set MyWanted=2]

[*sbcall snReturn=dsMyList,MyWanted,MyValue]

! Value set to [MyValue]

will output:

! Value set to 5