The purpose of this FAQ is not to teach you how to write UDFs but rather how to incorporate them in your script.
When KiX pre-parses the script, it will catalogue all the UDFs. They are defined with Function at the start and EndFunction at the end. When KiX parses through the second time and actually interprets (executes) your script, it will skip over all the UDFs.
Basically, there are two common places to put the UDFs.
1. Place the UDF code directly in your script. Where in the script doesn't really matter. Some like it on top while others (me included) prefer the bottom. Here's a short example using one of sealeopard's UDFs.
Code:
break on
$RC = DiskSpace()
"Disk Space = " + $RC ?
get $_
Exit 1
function diskspace()
if @INWIN=1
$diskspace=getdiskspace(expandenvironmentvars('%SYSTEMROOT%'))
else
$diskspace=getdiskspace('C:\')
endif
endfunction
You may notice that the UDF was placed after the EXIT but that does not prevent it from being pre-parsed. It could just as easily be place at the top and it would not be necessary to branch around it with a GOTO.
2. Place one or more UDFs into separate library files. Any extension can be used but I suggest KiX be used for scripts and UDF or KXF reserved for UDF libraries. These files can then be called from your script just as any sub-script can be called from another script. In this case however, placement of the call is crucial. The file that contains the UDF must be called before the UDF can be used.
Code:
break on
;placing the CALL here works
call @ScriptDir + "\diskspace.udf"
$RC = DiskSpace()
"Disk Space = " + $RC ?
get $_
Exit 1
;placing the CALL here doesn't work
call @ScriptDir + "\diskspace.udf"
Edited by Les (2005-09-08 08:21 PM)
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.