Page 1 of 1 1
Topic Options
#70371 - 2002-09-30 03:26 PM modifying variables in a function
BrianTX Offline
Korg Regular

Registered: 2002-04-01
Posts: 895
Notice that if I construct a function:

Function hello($c)
$hello=$c*5
Endfunction

The function returns the value of $c * 5

If I want to have a variable $c that is function *hello* of what it was before I would use:

$c=hello($c)

However, what if I wanted the function to modify the value of $c internally?

Function hello($c)
$c=$c*5
$hello=$c
Endfunction

If I use $c=hello($c) the function still works, but what if I wanted to change $c while I was filling another variable with hello($c)?

$d=hello($c) would work to give me the correct value, but $c would not change. Is there a way to allow the variable passed to the function to actually change without passing it as the result of the function?

By default, all variables declared in functions are LOCAL... it would be nice if they could be made to a GLOBAL so that you could actually change the variable even though you were returning something else:

$c=1,2,3,4,5,6,7

function hello(Global $c)
DIM $u
$u=ubound[$c]
$hello=$c[$u]
$c=REDIM PRESERVE $C[$u-1]
endfunction

This function would return the value 7, but the $c would be modified to 1,2,3,4,5,6.

Presently, I believe this type of functionality is unavailable. I realize that you could return an array with the first element of the array being an array, but this is not desirable in many cases. Is there a way around this, or should I add this to the suggestion box?

Brian

Top
#70372 - 2002-09-30 04:12 PM Re: modifying variables in a function
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
You mean something like:
code:
$c=1,2,3,4,5,6,7

$value = hello("$$c")
? "Value = " + $value
? "Ubound = " + ubound($c)
for each $x in $c
? $x
next

function hello($x)
DIM $rc, $u

$rc = execute ("$$u=ubound($x)")
$rc = execute
("
$$hello=$x[$u]
REDIM PRESERVE $x[$u-1]
")
endfunction


_________________________
Home page: http://www.kixhelp.com/hb/

Top
#70373 - 2002-09-30 04:17 PM Re: modifying variables in a function
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Took me a minute but basically what your talking about is passing values by reference (vb = byref) as in:

function ( $x byref, $y byval)

this has been suggested in the past ... brought up numerous times ... especially when kholm and myself were working on those sorting routines (COMBSORT and QSORT) ... and the discussion about QQSORT centered around using a global array.

But I think I like your syntax though better than the VB one ... especially since Ruud specifies the parameter qualifiers before the values as in:

function myfunc ( optional $c)

so why not:

function myfunc ( optional global $c)

seems to fit better into the language imho.

Top
#70374 - 2002-09-30 04:17 PM Re: modifying variables in a function
Jack Lothian Offline
MM club member
*****

Registered: 1999-10-22
Posts: 1169
Loc: Ottawa,Ontario, Canada
I believe what you want is a "subroutine" call not a function call. In most languages, subroutine calls pass both inputs & outputs but don't return values throught the subroutine name. Typically, inputs & outputs are distinct variables, although you can modify an input variable. This is usually not recommended. In most programming languages functions typically do not pass outputs except through the function name. Usually, you can not modify the values of inputs.

We had a debate on a issue very related to this in the suggestion forum & Richard came up with a creative way to pass multiple outputs. I don't know if it still works under version 4.12 but you might like to read this.

Discussion of adding a subroutine call

[ 30. September 2002, 16:20: Message edited by: Jack Lothian ]
_________________________
Jack

Top
#70375 - 2002-09-30 04:35 PM Re: modifying variables in a function
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Shawn, we can pass variables by reference..sort of. See my code example above.

[ 30. September 2002, 16:36: Message edited by: Howard Bullock ]
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#70376 - 2002-09-30 04:41 PM Re: modifying variables in a function
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
I'd avoid "Global" as the keyword, as the variable is not global. It it was you wouldn't need it in the function declaration.

Howard's solution is a good one.

You can simplify the code in your function (at the cost of not actually having a handle on the original variable) by creating local copies and assigning them back before returning:

code:
Function MyUDF($ByRef_A,$ByRef_B,$ByRef_C)
Dim $rc,$A,$B,$C
$rc=Execute("$$A=$ByRef_A")
$rc=Execute("$$B=$ByRef_B")
$rc=Execute("$$C=$ByRef_C")

... Your Code ...

$rc=Execute("$ByRef_A=$$A")
$rc=Execute("$ByRef_B=$$B")
$rc=Execute("$ByRef_C=$$C")
Return
EndFunction

This however will not work quite as well with some of the more complex COM object types.

Top
#70377 - 2002-09-30 04:50 PM Re: modifying variables in a function
BrianTX Offline
Korg Regular

Registered: 2002-04-01
Posts: 895
Shawn.. you hit exactly what I'm talking about. I hear all the discussion about functions returning a single value for each set of inputs (hence the definition of a mathematical function), however whatever you wish to call it, it would be nice to be able to return multiple values with a routine. Yes, this can be done with a subroutine (Using gosub and return), but applying the structure of a function to a subroutine is beneficial.

Perhaps using:
SUB a($x)
endsub

would make more sense, but because we already have:

function a($x)
endfunction

... why not just add the option to use:

function a(global $x)
endfunction

Does it really matter what you want to call it?

If you want to get really wild you could change creating functions or subroutines to something like:

DIM hello(global $x,$y,$z){}

It could be either function or subroutine..

(Is my thinking way off here?)

Brian

Top
#70378 - 2002-09-30 04:51 PM Re: modifying variables in a function
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Well, the nice thing is that (by their very nature) COM objects passed to functions are passed by reference ... can't make copies (even shallow ones) of those.
Top
#70379 - 2002-09-30 04:58 PM Re: modifying variables in a function
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Richard ... more to your point ... global would be a bad name ... byref makes more sense.
Top
#70380 - 2002-09-30 05:00 PM Re: modifying variables in a function
BrianTX Offline
Korg Regular

Registered: 2002-04-01
Posts: 895
Yeah.. you're probably right.. byref makes more sense.. but regardless.. the ability to pass references rather than just values would be nice.

But... can it still be considered a function if the original values are modified?

Brian

Top
#70381 - 2002-09-30 05:01 PM Re: modifying variables in a function
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Sorry Brian, I thought you had a specific need that required a solution.

KiXtart is a great tool, but I fear that too many enhancements will bloat the code to a point where it loses its advantage of having a small footprint. A mechanic's toolbox contains contains more than just one wrench. In fact it contains many different tools of varying sizes. Maybe we should be reaching into the toolbox for the right tool instead of always trying to make KiXtart into something its not. Just my two cents.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#70382 - 2002-09-30 05:04 PM Re: modifying variables in a function
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
quote:

But... can it still be considered a function if the original values are modified?

heh - thats a good, philosophical question !

Top
#70383 - 2002-09-30 05:08 PM Re: modifying variables in a function
BrianTX Offline
Korg Regular

Registered: 2002-04-01
Posts: 895
Actually, I was working on a sorting algorithm that would have made this useful. So, there are definitely practical applications to this. I'll bet that at least 25% of scripts could benefit from this functionality immediately (if they were rewritten). Adding FUNCTIONS to KiXtart has completely changed our way of coding complex solutions, and this would just be a slight extension of that.

Brian

[ 30. September 2002, 17:09: Message edited by: BrianTX ]

Top
#70384 - 2002-09-30 05:24 PM Re: modifying variables in a function
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
And to Howards point ... at what point does KiXtart become BloatWare ?

I mean obviously the exe size has steadily grown. Version 3.47 was about 130KB and 4.12 is now about 229KB. So yeah, its grown quite a bit but was the extra 98K of size worth all the extra features we have now ? I don't know the answer to that ... its pretty subjective I guess.

In terms of speed ... can't really argue with the fact the Kixtart is much, much faster now then it was before - although I have no figures to back that up.

Sure ... KiXtart started as a simple reskit utility for login scripting ... obviously its now much more than that (re: com and floating point support) ... is Kixtart a full featured scripting language ? Well if it isn't - its pretty darn close ... why shouldn't we go all the way ?

What I would like to know is what Ruud has to say about the future of KiXtart ... should he just sit back and rest for 7 days (sorta speak) ... or maybe move on to other things ? Or should he grow the language forward ... and if so ... in what direction.

Actually - remember that Interview that Ruud gave a while back - shortly before 4.0 got release ... future Kixtart directions kinda talk. Was thinking this just the day ... think its time we (maybe this board) sponsored another interview with Ruud ... just to see what he has planned.

Top
#70385 - 2002-09-30 05:28 PM Re: modifying variables in a function
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Brian,
This topic does not really relate to COM (although it could) and by it's nature seems more to be a "Suggestion" topic. It therefore, rightfully belongs in the suggestions forum.

{edit} [Embarrassed] oops, thought for a minute this was in the COM forum... never the less, is turning into more of a "Suggestions" thing...

[ 30. September 2002, 17:32: Message edited by: LLigetfa ]
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#70386 - 2002-09-30 05:39 PM Re: modifying variables in a function
BrianTX Offline
Korg Regular

Registered: 2002-04-01
Posts: 895
I'm not sure about when KiXtart becomes "bloatware", but there is something there are a few points I'd like to make:

1. We are using KiXtart to do everything from mapping drives to adding entries to a database to calculating mathematical functions.

2. As Microsoft releases new operating systems, KiXtart MUST support them with new versions, whether it be 100kb or 500kb.

3. The price for high speed connections is approaching the price for dial-up connections, so size is not as big of an issue as it once was.

4. Older versions of KiXtart are still available (and I assume will continue to be.)

.....
For the time being, if the size of KiXtart is truly an issue, then older versions can be used. Most people using KiXtart have multiple methods of deployment, whether it be distributed via logon script or on a software image. The only real issue with size comes when sending over dialup....

Are the additional features really worth increased size penalty? I'm not sure that they are in every case, BUT if KiXtart was getting too large, I'm sure we'd here complaints about it, and I haven't heard much in that regard.

Brian

Top
#70387 - 2002-09-30 05:43 PM Re: modifying variables in a function
BrianTX Offline
Korg Regular

Registered: 2002-04-01
Posts: 895
Perhaps it is more of a suggestions thing.. I don't know. I didn't realize we would get into the philosophy of it all. I wasn't merely making a suggestion, but discussing "scripting", best practices, what to look for in a scripting language... things like that.

I wasn't necessarily suggesting a certain course of action so much as testing the waters on coding practices.

Brian

(I certainly would have no ill will towards someone moving this to the suggestions forum)

[ 30. September 2002, 17:44: Message edited by: BrianTX ]

Top
#70388 - 2002-09-30 05:56 PM Re: modifying variables in a function
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Well hey guys - you know how things go around here ... one thing leads to another - then to another - then bam ... your way off topic [Wink]

I think the three questions I would ask of Ruud are:

1) Will you be growing the Kixtart scripting language any further than Version 4

2) If yes ... will you be growing it along the lines of a better LOGIN scripting language ... or a better GENERAL PURPOSE scripting language.

3) If growing it along the lines of a better GENERAL PURPOSE scripting language ... what new goodies are you planning for Version 5. If your growing it along the lines of a better LOGIN scripting language ... hmmmm ....

-Shawn

Top
#70389 - 2002-09-30 07:17 PM Re: modifying variables in a function
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
I just LOVE this board. [Big Grin] Brian, you will find my name on several suggestions that are not necessarily logon script related. I am sure that those suggestions would add substancially to the code base. [Wink]

Sorry, I just like to make sure everyone still has a pulse out there. [Razz]

[ 30. September 2002, 21:14: Message edited by: Howard Bullock ]
_________________________
Home page: http://www.kixhelp.com/hb/

Top
Page 1 of 1 1


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

Who's Online
0 registered and 601 anonymous users online.
Newest Members
M_Moore, BeeEm, min_seow, Audio, Hoschi
17883 Registered Users

Generated in 0.178 seconds in which 0.128 seconds were spent on a total of 12 queries. Zlib compression enabled.

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