Funprog

Use Funprog to declare a function called by func. This function can accept a list of arguments and has to return a result.

Syntax

 Funprog FUNCTION_NAMEFunprog FUNCTION_NAME ( ARGUMENT_LIST)

Examples

# Converts a string to another string, returns a status that can indicate that something got wrong# The initial string can be sent as a computed expression or as a variable (a copy is done in an INITIAL local variable)Funprog CONVERT(INITIAL,FINAL)Value Char INITIAL() Variable Char FINAL()...End STATUS# Receives a matrix as input (the matrix cannot be modified), returns a numeric valueFunprog COMPUTE_DETERMINANT(MATRIX)Const Decimal MATRIX(1..,1..,1..)Local Decimal DETERMINANT...End DETERMINANT# Receives an array of bytes, returns a short integer.# As the declaration is done, nothing prevents the code to modify BINARY_FIELD (which has been passed as reference)Funprog CHECKSUM(BINARY_FIELD)Variable BINARY_FIELD(1..)Local Shortint CHECKSUM...End CHECKSUM# Receives a field of variable type and converts it to string, returns a status if necessaryFunprog CONVERSION(FIELD,STRING_VALUE)Variable STRING_VALUE()Case type(FIELD)...EndcaseEnd STATUS# No argument, returns the current date and time in LAFunprog GET_TIME_IN_LALocal Datetime COMPUTED_TIME...End COMPUTED_TIME# Receives an array of strings, returns a clobFunprog AGGREGATE(CHAR_ARRAY)Const Char CHAR_ARRAY()(..)Local Clbfile RESULT(2)...End RESULT# Receives an array of integers, but will only consider the 10 first# If the argument of the call was defined by Integer MYARRAY(-20..30),# only MYARRAY(-20..-11) will be visible and mapped with INT_ARRAY(1..10)Funprog COMPUTATION(INT_ARRAY)Const Integer INT_ARRAY()(1..10)Local Integer RESULT...End RESULT# Receives an array of integers, with a shift in the index. The whole array is visible.# If the argument of the call was defined by Integer MYARRAY(-20..30),# MYARRAY(-20..30) will be visible and mapped with INT_ARRAY(1..51)Funprog COMPUTATION(INT_ARRAY)Const Integer INT_ARRAY()(1..)Local Integer RESULT...End RESULT
The `Funprog` instruction is followed by the declaration of the argument present on the argument list. These declarations are not mandatory and allows you to receive an argument of variable type. However, if they are present, they must be placed just after the `Funprog` declaration (in any order).

The declaration has the same syntax as usual variable declarations, except:
* They have to be prefixed by one of the following keywords: Variable, Const, or Value.
* If arrays are declared, the dimensions can be omitted, and only commas define the number of dimensions. The initial value is by default 0. An initial index value can also be given with a syntax such as 2.. if the index starts at 2. This means that a shift in the index is automatically done. You can also limit the number of elements that are seen in the Funprog by giving an end range in the argument declaration.
* The declaration of type can be one of the usual ones (Tinyint, Date, Shortint, Integer, Float, Double, Decimal, Char, Schar, Clbfile, Blbfile, Uuident, Datetime, or Instance).

The arguments declared are seen by the code of the Funprog as local variables. A Funprog ends with the following instruction: EndRETURN_VALUE, where RETURN_VALUE is the expression of the value returned (it can be of any type).

The keyword used for the declaration of the argument defines how the argument is managed:
* Value means that the argument is copied in a local variable. Any modification made in the Funprog are not sent back. This is the only possible mode if the argument is a calculated expression. Transmitting large arrays with this method will consume time and memory.
* Variable means that the argument is sent as a reference. A local variable that points on the argument is created. Any modification done changes the value of the calling argument.
* Const means that the argument is transmitted as a reference, with a read-only constraint. Any modification attempt in the Funprog will create an error, which means that the argument cannot be modified in the function. This is the mode used when no type declaration is made for an argument.

An argument sent in func can have one of the following formats:
* Any expression, which assumes that the argument is declared as Value.
* A single variable given by its name. If this variable is a multidimensional array, all the dimensions are transmitted by default. This is possible for arguments declared as Const, Variable, or Value.
* A subrange of an array where ranges are given with the RANGE_START..RANGE_END syntax. This is also possible for arguments declared as Const, Variable, or Value.

The maximum length of the Schar, Char, Blbfile, and Clbfile arguments are not given. You just need to follow the name of the argument by () (before the dimension, if you have some).

If the data type of an argument is not given, it is considered by default as an Integer if the calling argument is of type TinyInt, Shortint, or Integer. For all other types, the type remains the calling type.

For more information, see the func documentation.

Comments

At execution time:

Associated errors

Error codeDescription
32Transaction started in another func/call nesting level.
55The number of dimensions does not fit the parameter sent.
61Already an existing variable (two arguments have the same name).

See also

func, Value, Variable, Const, Call, End.