The following example shows a function procedure, called Average, in the Procedures View.
In this example, Average is a function procedure. The procedure was written to calculate the average of numbers in an array and return a double data type value. You can supply an array of any numbers. Note that a function procedure can only return a simple type, not a type of array or structure.
The procedure uses Val type parameters (ad, lSize) that accept numbers passed by value to the procedure. It is important to note that the lSize parameter is an optional parameter. If the optional parameter is not passed, the code indicates that lSize has an initial value of -1, which was set in the Value page of the Procedure Properties window.
The code indicates that if lSize is equal to -1 and lSize was not passed, then the procedure calculates the average using the size of the array that you supply and using the sizeof operator.
If lSize is not equal to -1, then lSize was passed and the procedure calculates the average using the values in lSize.
The procedure also uses local variables (variables d and i) that are used only within the procedure. The variable d is used to accumulate the sum of the values and to calculate the average. The variable i is the index used to step through the array values you supply.
For more information on the Procedures View, see Procedures View.
The following are examples of how you can call a value returned by a function procedure, called adResult:double[100].
The following calculates and returns the average of an array of 100 values:
dResult=Average (adResult)
The following calculates and returns only the average of the first 10 values:
dResult=Average(adResult,10)