Any idea how to return multiple variables from a function in ActionScript 3?
Anything like VB.NET where you can have the input argument's variable modified (ByRef arguments)?
Sub do (ByRef inout As Integer)
inout *= 5;
End Sub
Dim num As Integer = 10
Debug.WriteLine (num) '10
do (num)
Debug.WriteLine (num) '50
Anything apart from returning an associative array?
return {a:"string 1", b:"string 2"}
It appears that Strings, ints, units, Booleans are passed by Value. I tried this little snippet in Flash and the results were negative:
So... is String a blacklisted data type too? Boolean too? I mean whats a sure way of telling which types are passed by reference?
Wrong Wrong Wrong and Wrong.. every Argument is passed by value!!! the fact you can change a property inside the object passed doesn't mean you can change the object itself. try the following code
and here's the trace result :
Everything in AS3 is a reference aside from [u]ints. To generalize, everything that inherits
Object
will be given to the function by a reference.That being said, the only way I believe you can do it is use a container class like an
Array
or aString
("5" and do the conversion+math).It's all by value, if you understand C programming you will be familiar with the concept of pointers.
Think about a pointer as pointing to something in memory, and all variable names "bob from (bob = new person();)" Are essentially pointers that you work with.
Now, when you declare a function, since they are all By Value
You can think about both "a" and "b" being new pointers, so only within the "Test" function do both "a" and "b" exist and point to something in memory.
So let's use it
So the s1 and s2 pointers will ALWAYS point to "null" and "a new Sprite in memory" respectively, unless they are modified as s1 and s2 within their "Scope" <- Please make sure you understand variable scope before even trying to tackle this.
And within the function we now have two new pointers "a" pointing to "null" and "b" pointing to "the same sprite in memory as s2". so Since objects and arrays are essentially collections of pointers and only two new pointers have been created by the function for use "a" and "b" any properties/exposed variables "pointers to data in memory" of "a" or "b" will still be exactly the same as the ones for "s1" and "s2" and are the exact same pointers.
So within the function when "a" gets set to be "b", really all that happens is the "a" pointer now points to the same thing as "b". But "s1" and "s2" still point to what they were pointing to before.
!!!! If this was by reference you would not be able to think of "a" and "b" as new pointers, they would actually be "s1" and "s2" themselves, except you write them out as "a" and "b".
Note the subtle difference between DarthZorG's example and this one from the Flash docs:
Point Being: You can't change what the reference is pointing to but you can change the data that the reference is pointing to, so long as that reference is an Object/Array.
Quoting a googled source:
Which led me to look up the canonical source.