The FUNCTION command is new and still has some quirks. This is what I have found so far:Format: FUNCTION is very particular about it's formatting...
FUNCTION name [([OPTIONAL] arguments...)]
[statements]
[$name=expression]
ENDFUNCTION
1. Anything in [ ] is optional.
2. Anything in bold must be included except anything that falls under rule 1.
Parameters
name
Required string that defines the name of the function. KiXtart considers this to be a function just like and other KiXtart function. Builtin 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 text 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.
(keywords:
String = "hello", $var1(="text"), output of string functions eg: chr(65)
Number = 12, $var2(=number), output of number functions eg: val("12")
Text = "what i tell you", "what else i tell you"
Variable declaration = $var1, $array[1], $array[$i]
Script = any KiXtart script code
)
Remarks
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.
You cannot define a function inside another function. The RETURN and EXIT commands immediatly 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
EndFunction
Note: all KiX commands and functions are case INsensitive.
MCA,
This is my slightly modified version of your script:
MCAfunction.k2k
code:
break on cls
$max=10
"running $$a=valid function with params..." ?
$a=count($max)
"$$a = " $a ? ?
"running valid function with params..." ?
count($max) ? ?
$max=11
"running $$a=invalid function with params..."
$a=count2($max) ?
"$$a = " $a ? ?
"running invalid function with params..." ?
count2($max) ? ?
"running $$a=invalid function without params..." ?
$a=count2
"$$a = " $a ? ?
"running invalid function without params..." ?
count2 ? ?
function count ($max)
$cnt=0
for $i=1 to $max step 2
$cnt=$cnt+$i
next
$count=$cnt
endfunction
Here is the result
quote:
running $a=valid function with params...
$a = 25running valid function with params...
25
running $a=invalid function with params...11
$a = count2
running invalid function with params...
Script error : unknown command !
count2($max) ? ?
running $a=invalid function without params...
$a = count2
running invalid function without params...
Script error : unknown command !
count2 ? ?
Notice that "running $a=invalid function with params..." returned 11 then $a="count2", this is because of two things:
1. The count2() was ignored and KiX displayed $max (which was 11.. see line 12 of the code)
and
2. $a was set to "count2" because KiX ignored the ($max) and saw this as a standard $var=value statement.
Notice that "running $a=invalid function without params..." did the same, but without reason 1 - there was no $max to display. Reason 2, the $var=value statement, still occurred.
Both the "running invalid function..."'s failed because "count2" with or without () is not a valid command or function.
The reason KiX processed the $a=invalid function is because of KiX's lax syntax checking and undefined variable types. You could never have done that in Delphi for example as all Variables and functions need to be fully defined.
hope this helps.
cj
p.s. The FUNCTION information above is copied from my version of the KiX user manual. I am re-formatting it to use bold and italics to show how KiX commands are constructed etc... Once it is done, I will make a copy available to all.
------------------
For more scripts goto my website and click the hammer and spanner icon.
chrismat@ozemail.com.au
[This message has been edited by cj (edited 05 April 2001).]