Please enable JavaScript to view this site.

DAQFactory User's Guide

Navigation: 5 Sequences & Scripting

5.17 Using sequences as functions

Scroll Prev Top Next More

Sequences typically run standalone in their own thread.  But sequences can also act as functions, being called from another sequence or even an expression.  In this case, the sequence runs in the thread of the calling sequence and the calling sequence waits until the function sequence is complete.  To call a sequence from another sequence simply name the sequence followed by a list of optional arguments in parenthesis:

 

SequenceName(Input,3)

 

or

 

SequenceName()

The arguments appear in the called sequence as Private variables.  By default, the arguments are named arg0, arg1, etc..  but you can tell DAQFactory to name them something different by putting a function declaration line at the top of the sequence. For example:

AddTwo sequence:

 

function AddTwo(valuein)

  return(valuein + 2)

This is a simple function that takes an argument named valuein, adds to this value and returns the result.  We can then call it from another sequence:

Main sequence

 
private X = 1

X = AddTwo(X)

? X   // displays 3

This sequence creates a variable named X, passes it as a parameter to our function, and sets the result back in X.  Finally it prints the value of X to the command window.

The function declaration includes the reserved word "function", followed by the name of the sequence, and then a comma delimited list of argument names in parenthesis.  If the argument is a string argument you should add the word string before the argument name:

 

function MyFunc(NumVar, string MyStringVar)

If you do not provide a function declaration, or you pass more arguments to a function then specified in the function declaration, the default names of arg0, arg1, etc. are used.  So, our AddTwo sequence above without a function declaration becomes:

AddTwo sequence:

 

return(arg0 + 2)

It is recommended that all functions have a function declaration so that your arguments have names that make sense.  The exception would be when you want to create a function that takes a variable number of arguments.  There are several different situations where this occurs:

1) You want to create a function with optional arguments.  In this case, you will want to put all the arguments in the function declaration, and then examine any optional ones to see if they were passed by using the isempty() function and setting the default value if not.  Note that all the optional arguments must be at the end of the argument list:

 

function IncrementBy(x, by)

   if (isempty(by))

     private by = 1

   endif

   return(x+by)

In this example, IncrementBy(4) will return 5 because by is optional and defaults to 1. IncrementBy(4,2), however, will return 6 because by was specified.

2) You want to create a function with a variable number of arguments.  For this an additional private is also created inside of every function called argc which contains the number of arguments passed.  So:

 

function SumVars()

  switch

    case (argc == 2)

      return(arg0 + arg1)

    case (argc == 3)

      return(arg0 + arg1 + arg2)   

    default

      System.ErrorMessage("Invalid # of parameters")

    endcase

This sample looks at the number of arguments and returns the sum of 2 or 3 values or an error.  Notice how we avoided the name "Sum" as a function.  This is a reserved DAQFactory function and cannot be used as a sequence name.

3) You want to create a function with several defined arguments and a variable number of arguments in addition to the defined arguments.

 

function PrintVars(string Prefix)

  switch

    case (argc == 3)

      return(Prefix + DoubleToStr(arg1 + arg2))

    case (argc == 4)

      return(Prefix + DoubleToStr(arg1 + arg2 + arg3))   

    default

      System.ErrorMessage("Invalid # of parameters")

    endcase

Notice that are arg notation starts at 1 because Prefix takes the 0 spot.  However, the argc argument total still includes Prefix.

Note: All arguments are passed by value and not by reference.  For example, calling our first example above: AddTwo(ChannelX[0]), the value of ChannelX[0] is passed to the AddTwo function, not ChannelX[0] itself.  Changes to valuein / arg0 in the AddTwo function do not affect ChannelX.  Only by assigning the return value of AddTwo to ChannelX are we able to change ChannelX as seen in the Main sequence above.

Note: You can pass a maximum of 20 arguments to a sequence.