Howdy all-
Was working on a macro or two, and came up with the following question - can we pass arrays to functions? It would seem to be a very useful thing to be able to do. Thanks in advance...
Moderator: MacroQuest Developers


Code: Select all
/varset a(0,0) 1
/varset a(0,1) 2
/varset a(0,2) 3
Code: Select all
/echo $a(0,0) $a(0,1) $a(0,2)Code: Select all
sub main
/call aSub 1 2 3 "a friend"
/return
sub aSub
/echo $p0, $p1, $p2, $p3, $p4.
/returnNotice that the last varialbe is the same as it being blank, this is because it is set ot "". I hope this helps.[MacroQuest] 1, 2, 3, "a friend", .

Yes, I figured as much, but I well and truly hate global variables. They seem to have a way of doing more harm than good. Thats why I like to pass variables. If you use global variables, then all your scripts have to use the set ranges for different things, and you have to be very careful about not stepping outside the range, or even stepping on another functions variables. Which is why I like to avoid globals. In some cases, yes, they are useful - even necesarry, but the reason I asked was because, like I said, I like to avoid them. Another style thing. :)L124RD wrote:Salutations,
if you doyou can access the variable in the function you call withCode: Select all
/varset a(0,0) 1 /varset a(0,1) 2 /varset a(0,2) 3you don't have to pass it with an argument in /call.Code: Select all
/echo $a(0,0) $a(0,1) $a(0,2)
Aye, that is what I was looking for... the p# variables are well and truly local, unlike the v#'s which are global. Just wanted to make sure that all of them got reset, else I could be looking at some 'fun' info. :)L124RD wrote: As for the p# variables, they are all set to "" unless it is set in /call iewill echoCode: Select all
sub main /call aSub 1 2 3 "a friend" /return sub aSub /echo $p0, $p1, $p2, $p3, $p4. /returnNotice that the last varialbe is the same as it being blank, this is because it is set ot "". I hope this helps.[MacroQuest] 1, 2, 3, "a friend", .

Well, that could work, but it also takes away a (potentially) large chunk of the array space as well. If the p#'s are forcibly reset on each /call, (which I believe you said they were...), I'd probably start at the top of the p#s and work my way down for 'local' variables (/for counters and other locally-calculated variables). That way I wouldn't have to worry about any data collisions in global-space. Assuming, of course, that I can set p# variables. :)L124RD wrote:well, if you want each per function, you could use the array variables as such. a(0,#) for sub main, a(1,#) for use in the next sub, etc on your way down (add new subs to the bottom) you use the p# variables to get things passed between subs and the a(#,#) for the local variables, though not truly local, this could be something...