Page 2 of 2 <12
Topic Options
#37531 - 2006-02-28 12:46 PM Re: GOSUB vs. FUNCTION
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Just another addition from personal experience:

Go with functions, you will find out that they are waaay more flexible than subroutines.

(edit: I think the fact that a subroutine call from inside a function fails has got something to do with the fact that functions are being preparsed (one of the things that make them more flexible and reusable) )


Edited by Jochen (2006-02-28 12:48 PM)
_________________________



Top
#37532 - 2006-02-28 01:04 PM Re: GOSUB vs. FUNCTION
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
no, they might be reusable but they certainly are not as flexible as subroutines.
_________________________
!

download KiXnet

Top
#37533 - 2006-02-28 01:20 PM Re: GOSUB vs. FUNCTION
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Proofs ?

[Hmmm .. ok, one could consider the use of local scope vars in subs, but still you can pass them as arguments to the function]

I am probably too long out of the subroutine business to see the felxibility, still functions are the better way to go.


Edited by Jochen (2006-02-28 01:26 PM)
_________________________



Top
#37534 - 2006-02-28 01:32 PM Re: GOSUB vs. FUNCTION
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
btw,

I might found out why it is not possible to jump into a subroutine from inside a function:

Quote:


The Return statement causes an immediate exit from a Function procedure. Program execution continues with the statement that follows the statement that called the Function procedure. Any number of Return statements can appear anywhere in a Function procedure.





On preparsing the following happens:

-> function
-> entering sub
-> return (exits not only the sub but also the function)
-> same function again (two times function without endfunction in between causes Kix to choke on "missing endfunction"
_________________________



Top
#37535 - 2006-02-28 02:00 PM Re: GOSUB vs. FUNCTION
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
The reason is actually much simpler.

The label is in the local scope, so is simply not visible in the function.

You can declare the same subroutine within the function itself, and then it will work.

Top
#37536 - 2006-02-28 02:05 PM Re: GOSUB vs. FUNCTION
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
oh well ... but then it shouldn't fail on "missing endfunction", no?

Hmmm ... yes, it wil work if the subroutine is inside the function, but apart from the fact that it is nonsense to have that, it will run the sub twice and also exit the function on 'return'

edit2:

no it won't... this works as expected

Code:

break on

test()

get $
exit 0

function test()
gosub test1
"still in function" ?
exit 1
:test1
"Inside sub" ?
return
endfunction



I can't think of a case were one would need this but still, it works


Edited by Jochen (2006-02-28 02:22 PM)
_________________________



Top
#37537 - 2006-02-28 04:47 PM Re: GOSUB vs. FUNCTION
Rosario Carcò Offline
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 Offline
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ò Offline
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
Page 2 of 2 <12


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 1003 anonymous users online.
Newest Members
StuTheCoder, M_Moore, BeeEm, min_seow, Audio
17884 Registered Users

Generated in 0.132 seconds in which 0.098 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org