#37537 - 2006-02-28 04:47 PM
Re: GOSUB vs. FUNCTION
|
Rosario Carcò
Fresh Scripter
Registered: 2002-08-28
Posts: 8
|
Quote:
b) The SUBROUTINES may alter variables which were declared as globals whilst functions, as explained in the previous posts, have no chance to do so.
I apologize for that lapsus. Of course we must consider and distinguish global variables and values passed as parameters to a function call. In KIX the latter are passed by value, hence as copies, and are destroyed on exit from the function.
I am working on a function calling another function, as calling a subroutine with GOSUB seems not to work from inside a function. The KIX manual states that GOSUB "label" can use also an expression. So my first approach was to pass the "label" as parameter to a function, like this:
Code:
doScheduledTask($startY, $startM, $startD, $nameOfSubOrFunction) ;call function
;Function declaration FUNCTION doScheduledTask($startY, $startM, $startD, $nameOfSubOrFunction) ;depending on parameters $startY, $startM, $startD, etc. call sub or function
GOSUB $nameOfSubOrFunction ;this is syntactically correct, but subs can not be called here
$rc = $nameOfSubOrFunction ;so I modified all the code to use a function instead ENDFUNCTION
;Function declaration FUNCTION testTask() ? "we are in testTask() now" ENDFUNCTION
In the latter form the desired function IS NOT CALLED, if I pass
Code:
doScheduledTask(2006,02,28, "testTask()")
Maybe it is not possible to call a function by the "label" being an expression like GOSUB $end, as illustrated in the KIX manual?
Thanks for your help, Rosario
|
|
Top
|
|
|
|
#37538 - 2006-02-28 05:05 PM
Re: GOSUB vs. FUNCTION
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
You need to use "Execute()" to call functions using variable names.
Here is an example: Code:
Break ON $=SetOption("Explicit","ON") Dim $s $s="This is a string" "Start string is: '"+$s+"'"+@CRLF " In hex is: '"+udfFunction("udfHexString($$sParam1)",$s)+"'"+@CRLF " Reversed is: '"+udfFunction("udfReverse($$sParam1)",$s)+"'"+@CRLF Function udfFunction($sFunctionToCall,$sParam1) Dim $sDiscard $sDiscard=Execute("$$udfFunction="+$sFunctionToCall) Exit @ERROR EndFunction Function udfHexString($s) While $s<>"" $udfHexString=$udfHexString+Right("00"+DecToHex(Asc($s)),2) $s=SubStr($s,2) Loop EndFunction Function udfReverse($s) While $s<>"" $udfReverse=Left($s,1)+$udfReverse $s=SubStr($s,2) Loop EndFunction
|
|
Top
|
|
|
|
#37539 - 2006-02-28 05:43 PM
Re: GOSUB vs. FUNCTION
|
Rosario Carcò
Fresh Scripter
Registered: 2002-08-28
Posts: 8
|
Thanks a lot. I rewrote that piece of code, which works fine now, EVEN WITH PARAMETERS passed to the function we want to be called:
Code:
doScheduledTask(2006, 02, 28, "testTask('hello')") ;call function
;Function declaration FUNCTION doScheduledTask($startY, $startM, $startD, $nameOfFunction) ;depending on parameters $startY, $startM, $startD, etc. call function
$rc = EXECUTE($nameOfFunction) ;calling a function works fine, including params to that function! ENDFUNCTION
;Function declaration FUNCTION testTask($p1) ? "we are in testTask() now " + $p1 ENDFUNCTION
The final versions of my functions will be sort of
Code:
; doScheduledTask("<",startY,startM,startD,"functionName", ; OPTIONAL endY,OPTIONAL endM,OPTIONAL endD) ; [executes given Function before "<", exactly "=", after ">" ; or "!" inbetween given Date(s)] ; doTempMapping("usrName","<",startY,startM,startD,"X:", ; "\\uncPath\shareName",OPTIONAL endY, ; OPTIONAL endM,OPTIONAL endD) ; [maps given path to given Drive-Letter before "<", exactly "=", ; after ">" or "!" inbetween given Date(s) for given userName]
I only roughly checked the UDF Library. Maybe such code already exists.
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 1003 anonymous users online.
|
|
|