#70371 - 2002-09-30 03:26 PM
modifying variables in a function
|
BrianTX
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
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
|
|
Top
|
|
|
|
#70375 - 2002-09-30 04:35 PM
Re: modifying variables in a function
|
Howard Bullock
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 ]
|
|
Top
|
|
|
|
#70376 - 2002-09-30 04:41 PM
Re: modifying variables in a function
|
Richard H.
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
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
|
|
|
|
#70380 - 2002-09-30 05:00 PM
Re: modifying variables in a function
|
BrianTX
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
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.
|
|
Top
|
|
|
|
#70382 - 2002-09-30 05:04 PM
Re: modifying variables in a function
|
Shawn
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
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
|
|
|
|
#70386 - 2002-09-30 05:39 PM
Re: modifying variables in a function
|
BrianTX
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
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
   
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
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
KiX Supporter
   
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
I just LOVE this board. 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.
Sorry, I just like to make sure everyone still has a pulse out there. ![[Razz]](images/icons/tongue.gif) [ 30. September 2002, 21:14: Message edited by: Howard Bullock ]
|
|
Top
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 837 anonymous users online.
|
|
|