Function statement
Declare the name, parameters of the Function process, and the code that forms its body.
[Public[Default]|Private]Functionname[(
arglist
)]
[statements]
[name=expression]
[ExitFunction]
[statements]
[name=expression]
EndFunction parameter
Public
Indicates that the Function process can be accessed by all other procedures in all scripts.
Default
Use only with the Public keywords in the Class block to indicate that the Function procedure is the default method of the class. If more than one Default procedure is specified in a class, an error occurs.
Private
Indicates that the Function procedure can only be accessed by other procedures in the script that declares it, or if the function is a data class, the Function procedure can only be accessed by other procedures in the class.
name
The name of the function follows the standard variable naming rules.
arglist
A list of variables representing the parameters to be passed to the Function procedure when invoked. Separate multiple variables with commas.
statements
Any group of statements executed in the body of the Function procedure.
Expression
The return value of the function.
The arglist parameter contains the following syntax and parts:
[ByVal|ByRef]varname[()]
parameter
ByVal
Indicates that the parameter is passed in value.
ByRef
Indicates that the parameter is passed in reference.
varname
Represents the name of the parameter variable; follows standard variable naming rules.
illustrate
If Public or Private is not explicitly specified, Function procedures are public by default, i.e. they are visible to all other procedures in the script. The value of local variables in the Function is not preserved in the call to the procedure.
Function procedures cannot be defined in any other procedure (e.g., Sub or PropertyGet).
Use the ExitFunction statement to exit immediately from the Function process. The program continues to execute the statement after the statement calling the Function procedure. Any ExitFunction statement can appear anywhere in the Function procedure.
Similar to the Sub process, the Function process is an independent process that can obtain parameters, execute a series of statements, and change their parameter values. The difference from the Sub procedure is that when you want to use the value returned by the function, you can use the Function procedure to the right of the expression, which is the same as the internal function used, such as Sqr, Cos, or Chr.
In an expression, the Function procedure can be called by using the function name and then giving the corresponding parameter list in parentheses. For more information about calling the Function procedure, see the Call statement.
Warning Function procedure can be recursive, i.e. the procedure can call itself to complete a given task. However, recursion can cause stack overflow.