Can "Return" return more than 1 variable?
BlitzMax Forums/BlitzMax Beginners Area/Can "Return" return more than 1 variable?
| ||
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? |
| ||
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 |
| ||
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. |
| ||
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. |
| ||
Or return an array? |
| ||
the last solutions is more efficient if you can not know how many 'return' parameters you will have. |
| ||
^ Spiffy!Local A$[] = Foo() For B$ = EachIn A Print B Next Function Foo$[]() Return ["Foo","Bar","Baz","Quux"] End Function |
| ||
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... |