Func

Use this function to Call a subroutine executed with its own local variables class that returns a value. Parameters can be transmitted as values or as references; so that the parameters transmitted by reference can be modified by the subroutine.

Syntax

func SUBPROGRAMfunc LIBRARY.SUBPROGRAMfunc SUBPROGRAM(ARGUMENT_LIST)func LIBRARY.SUBPROGRAM(ARGUMENT_LIST)func =EXPRESSION_SUBPROGRAMfunc =EXPRESSION_SUBPROGRAM with(ARGUMENT_LIST)

Examples

 # Call of a routine that increments a valueLocal Integer II=314 : I=func ADD_ONE(I) : # I contains now 315...Funprog ADD_ONE(J)Variable Integer J : # Can be value, variable, or constantEnd J+1# Call of a function with parameters in another processLocal Integer MATRIX(1..10,1..20,1..30)...# Only a sub-array is transmitted (even if it is sent as a reference)RESULT = func MATRIC_LIBRARY.COMPUTE_SUM(MATRIX(5..6,2..4,1..20))...# In the MATRIX_LIBRARY script, the arguments areFunprog COMPUTE_SUM(MATRIX,RESULT)Const Integer MATRIX(,,)End sum(MATRIX)# Example of subprogram with a variable type of argumentLocal Char MY_STRING(20),TEXT(10)MY_STRING="abcdefghijklmnopqrst"I = func RETURN_TYPE(pi,TEXT): # Will return 7 (decimal) and "3.14159265"I = func RETURN_TYPE([1/1/2013],TEXT) : # Will return 3 (date) and "01/01/2013"I = func RETURN_TYPE(100,TEXT) : # Will return 4 (integer) and "100"I = func RETURN_TYPE("text sent",TEXT) : # Will return 523 (clob) and "text sent"I = func RETURN_TYPE(MY_STRING,TEXT) : # Will return 20 (string 10) and "abcdefghij"End# A subprogram that returns the data type of the argumentFunprog RETURN_TYPE(ARGUMENT,TEXT)Variable Char TEXT()TEXT=num$(ARGUMENT)End type(ARGUMENT)

Description

func executes a subroutine with possible parameters. The calling code ends its execution when an End instruction is encountered. The result of func is used in the calling instruction and the execution continues in sequence.

A func performed without specifying a script is done in the same script.

Comments

Variable scope

During a func, the following information is saved:
* Local Variables.
* The tables declared with the current values of the corresponding [F] and [G] classes.
* The group of the default class lists (outside of any local class).

During a func, the following information is lost:
* The tables reopened with the same abbreviation in the subroutine.
* The label associated with Onerrgo is not saved when passing to a subroutine even if this is performed in the same process.

When executing a func, a class of local variables is created with the subroutine, abbreviated [L]. This local class becomes the default class because the local class with the calling process is no longer accessible. When you return to the process, you will find the default local class which existed before the call.

The local variables and all the resources opened with Local keyword in the subroutine are lost after the execution of the call.

Parameters

The number of parameters transmitted in the func must correspond to the number of parameters declared in the Funprog instruction.

The parameters of the Funprog can be declared by using a data type prefixed by Variable, Value, or Const. They must be declared just after prefixing the data type that must be the same as the one sent in the func.

The parameters can be transmitted as:

The declaration of the parameters is not mandatory, and this allows you to send parameters of variable types. Make sure that they are transmitted as references on read-only values, exactly as if they would have been declared with Const.

The parameters can be variables or expressions. If a parameter is an expression, it can only be transferred by value; the Funprog parameter cannot be defined with dimensions.

A variable passed in parameters can be declared with dimensions. The number of dimensions must be the same. A sub-array can be transmitted as shown in one of the previous examples.

Miscellaneous

A func can be called recursively and the number of embedded func is not limited. Nevertheless you have to pay attention to the available memory as the number of opened table in Local mode for each call nested is viewed independently.

The execution of a func must return a value. End without value triggers an error if executed in a Funprog.

A func cannot be used in a condition of a Where sentence used in File, Filter, or Forstatements. It cannot be transmitted to the database.

Associated Errors

Error codeDescription
10The expression giving the script name does not return a string value.
20The process does not exist.
39The label does not exist.
55Number of dimensions invalid.
69Number of parameters does not correspond.
70Transmission mode incompatible.

See also

Funprog, Call, Subprog, End.