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.
func SUBPROGRAMfunc LIBRARY.SUBPROGRAMfunc SUBPROGRAM(ARGUMENT_LIST)func LIBRARY.SUBPROGRAM(ARGUMENT_LIST)func =EXPRESSION_SUBPROGRAMfunc =EXPRESSION_SUBPROGRAM with(ARGUMENT_LIST)
SUBPROGRAM
is the name of the routine declared by the Funprog keyword.LIBRARY
is the name of the script that contains the code of the routine called.ARGUMENT_LIST
is a list of expressions, variables, or subarrays that can be empty. The elements are separated by commas.EXPRESSION_SUBPROGRAM
is an alphanumeric expression whose evaluation returns the code of the routine called. The routine can be preceded by the library name with the nformat LIBRARY.SUBPROGRAM
. # 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)
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.
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.
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.
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.
Error code | Description |
---|---|
10 | The expression giving the script name does not return a string value. |
20 | The process does not exist. |
39 | The label does not exist. |
55 | Number of dimensions invalid. |
69 | Number of parameters does not correspond. |
70 | Transmission mode incompatible. |