View Full Version : Function + variable--> passing by variable??
issamneo
05-02-2005, 09:39 AM
hi
i want to pass 4 variables to a function and when i change those variable in this function they change in the code also something like this
var x=5;
var y=5;
function change(x , y){
x++;
y++;
}
change(x,y);
trace (x);
trace (y);
// now i want to see 66 not 55
i see this example but it gives only one return vars
function sqr(x):Number {
return x * x;
}
Tom Clark
05-02-2005, 11:25 AM
Pass them as an array:
var a = 2;
var b = 3;
var c = 4;
var foo = function(a, b, c) {
a *= a;
b *= b;
c *= c;
}
foo();
trace(a + "," + b + "," + c);
foo = function(array:Array):Array {
for(var i in array)
array[i] *= array[i]
return(array);
}
var array = new Array(2, 3, 4);
trace(array = foo(array));
outputs:
2,3,4
4,9,16
The first three didn't change, but the second three did. Actionscript doesn't have variable referencing (foo(&bar) in C/C++/et al.), but you can make due with arrays (of nth dimension).
Hmm what you want to do is pass the variable by "reference" (talking in C language here...) but I don't know if you can do that in Flash.
One thing you CAN do though, is use an Object. Objects seem to always be treated as references where variables aren't.
For example:
numbers = new Object();
numbers.x = 5;
numbers.y = 5;
function change(object)
{
object.x++;
object.y++;
}
change(numbers);
trace (numbers.x); //is now 6
trace (numbers.y); //is now 6
That is one way to get around your problem. I dont know if passing single variables by reference is possible but if you find out how let me know :)
-oka
issamneo
05-02-2005, 12:21 PM
thanks both for the reply
oka very good idea that's what i want.
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.