Can "Return" return more than 1 variable?

BlitzMax Forums/BlitzMax Beginners Area/Can "Return" return more than 1 variable?

Dubious Drewski(Posted 2005) [#1]
I have a function that computes two numbers. I would like
to return those two numbers to the main loop.

Can this be done without resorting to something silly like
using global variables or calling the function twice to return each number?


Leiden(Posted 2005) [#2]
Sure can, use Var's. For instance if you want function apple to return seed and pip then you would do something like:

Function Apple(applecount:int, seed:int var, pip:int var)

      seed = applecount * 5
      pip = seed * 1

End Function



ImaginaryHuman(Posted 2005) [#3]
In other words, you are then sharing the actual seed and pip variables between the main program and the function - both can see the same variable rather than there being the usual boundary. So you can use it to communicate.


Dubious Drewski(Posted 2005) [#4]
Oh, duh. Of course! So essentially, I'm passing the memory location of the original vars instead of creating new ones with the same values.


Azathoth(Posted 2005) [#5]
Or return an array?


degac(Posted 2005) [#6]
the last solutions is more efficient if you can not know how many 'return' parameters you will have.


WendellM(Posted 2005) [#7]
^ Spiffy!
Local A$[] = Foo()

For B$ = EachIn A
	Print B
Next

Function Foo$[]()
	Return ["Foo","Bar","Baz","Quux"]
End Function



Tom Darby(Posted 2005) [#8]
Remember, too, that a function can return a user-defined type--useful if you know exactly what you want to return but don't always want to send the variables you want to change through the function parameters...