FUNCTION
Type KiXtart 2001 only Command
Action Declares the name, arguments, and code that form the body of a Function procedure.
Syntax FUNCTION name[([OPTIONAL] arguments...)]
[statements]
[$name=expression]
ENDFUNCTION
Parameters name
Required string that defines the name of the function. KiXtart considers this to be a function just like and other KiXtart function. Built-in KiXtart functions cannot be replaced with this command.
arguments...
Optional comma separated variable declarations that are passed to this function when it is called.
statements
Optional script that will be executed when this function is called.
expression
Optional value returned to calling script.
OPTIONAL
Optional keyword that defines the following argument as optional, that is, it is not required. Every argument following the first optional argument must also be declared as optional.
Remarks A Function procedure is a separate procedure that can take arguments, perform a series of statements, and change the values of its arguments. A Function procedure can be used in an expression the same way KiXtart functions are. For example: $a=myfunction(1,2) The value of $a will be set to the return value of myfunction.
Function procedures have a global scope, that is, they are visible to all other scripts and procedures in the scripts. The value of local variables in a Function is not preserved between calls to the procedure.
Functions cannot be defined inside other functions. The RETURN and EXIT commands immediately exit the function and execution continues in the calling script.
Function procedures can be recursive, that is, they can call themselves to perform a given task, but excessive or continuous recursion can lead to stack overflow.
To return a value from a function, assign the value to the function name. Any number of such assignments can appear anywhere within the procedure. If no value is assigned to name, the procedure returns an empty value.
Variables defined within the function with DIM are always local to this function. Variables that are used but not declared in a function are declared as global variables and could conflict with other global variables. This rule does not apply to $name because this is the return value.
While statements and $name=expression are both optional, at least one of them is required at any one time. That is, they can’t both be missing.
Examples "The meaning to life is " ADD(13, 29) ; try this: ADD(11, 22, 9)
?
Function ADD($first, $second, OPTIONAL $third)
$ADD=$first + $second + $third
if $third $ADD=$ADD+$third endif
EndFunction