Can you pass arrays to functions?

Help section from before the user variable changes that broke all macros

Moderator: MacroQuest Developers

User avatar
DeathSpiral
a ghoul
a ghoul
Posts: 95
Joined: Thu Aug 22, 2002 6:31 pm

Can you pass arrays to functions?

Post by DeathSpiral » Tue Aug 27, 2002 1:40 am

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...

User avatar
DeathSpiral
a ghoul
a ghoul
Posts: 95
Joined: Thu Aug 22, 2002 6:31 pm

Post by DeathSpiral » Tue Aug 27, 2002 1:46 am

Also as a followup question... variable number of arguments to a function. It would seem possible, but how to do bounds checking? Each time we /call a function, does it reset all 100 (p0-p99) variables, or if it isn't provided, does the previous value stick? Thanks again in advance...

User avatar
L124RD
Site Admin
Site Admin
Posts: 1343
Joined: Fri Jun 14, 2002 12:15 am
Location: Cyberspace
Contact:

Post by L124RD » Tue Aug 27, 2002 5:07 am

Salutations,
if you do

Code: Select all

/varset a(0,0) 1
/varset a(0,1) 2
/varset a(0,2) 3
you can access the variable in the function you call with

Code: Select all

/echo $a(0,0) $a(0,1) $a(0,2)
you don't have to pass it with an argument in /call. As for the p# variables, they are all set to "" unless it is set in /call ie

Code: Select all

sub main
/call aSub 1 2 3 "a friend"
/return

sub aSub
 /echo $p0, $p1, $p2, $p3, $p4.
/return
will echo
[MacroQuest] 1, 2, 3, "a friend", .
Notice that the last varialbe is the same as it being blank, this is because it is set ot "". I hope this helps.

User avatar
DeathSpiral
a ghoul
a ghoul
Posts: 95
Joined: Thu Aug 22, 2002 6:31 pm

Post by DeathSpiral » Tue Aug 27, 2002 6:26 am

L124RD wrote:Salutations,
if you do

Code: Select all

/varset a(0,0) 1
/varset a(0,1) 2
/varset a(0,2) 3
you can access the variable in the function you call with

Code: Select all

/echo $a(0,0) $a(0,1) $a(0,2)
you don't have to pass it with an argument in /call.
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: As for the p# variables, they are all set to "" unless it is set in /call ie

Code: Select all

sub main
/call aSub 1 2 3 "a friend"
/return

sub aSub
 /echo $p0, $p1, $p2, $p3, $p4.
/return
will echo
[MacroQuest] 1, 2, 3, "a friend", .
Notice that the last varialbe is the same as it being blank, this is because it is set ot "". I hope this helps.
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. :)

User avatar
L124RD
Site Admin
Site Admin
Posts: 1343
Joined: Fri Jun 14, 2002 12:15 am
Location: Cyberspace
Contact:

Post by L124RD » Tue Aug 27, 2002 6:54 am

Salutations,
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...

User avatar
DeathSpiral
a ghoul
a ghoul
Posts: 95
Joined: Thu Aug 22, 2002 6:31 pm

Post by DeathSpiral » Tue Aug 27, 2002 7:18 am

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...
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. :)

So I guess that pretty much answers / addresses my whole local-use variables thing. I appreciate the answers, and for putting up with me... :)

Have a nice day.

User avatar
L124RD
Site Admin
Site Admin
Posts: 1343
Joined: Fri Jun 14, 2002 12:15 am
Location: Cyberspace
Contact:

Post by L124RD » Tue Aug 27, 2002 9:43 am

Salutations,
You can set p# variables, and you can't use bunny variables (a(#,#) vars in /for anyway... so i'd go with that...