Page 1 of 1
[SOLVED] JS. Transfer a variable to a function
Posted: Tue Jul 24, 2007 7:29 am
by Gente
Hi everyone.
Didn't expected it would be a problem but is. The question is how to transfer the variable to the function?
Or in other words what is the analog of
in JS?
I thought it's something like this
but it returns
missing formal parameter error.
Please share your experience if you met such situation.
Thanks.
Posted: Tue Jul 24, 2007 7:31 am
by feyd
You don't need the "var" keyword.
Posted: Tue Jul 24, 2007 7:42 am
by Gente
This way the value will be transfer, not variable. OK, example:
Code: Select all
var i = 0;
function inc(a)
{
a++;
}
inc(i);
alert(i);
Now it shows 0. I want 1.
Thanks.
Posted: Tue Jul 24, 2007 7:57 am
by miro_igov
How about this:
Code: Select all
var i = 0;
function inc()
{
i++;
}
inc(i);
alert(i);
Posted: Tue Jul 24, 2007 8:03 am
by Gente
It was just example. I can not use global variables because I would run this function several time with different parameters.
Posted: Tue Jul 24, 2007 8:08 am
by miro_igov
Other way is using return i;

Posted: Tue Jul 24, 2007 8:09 am
by Gente
And if I have the couple of variables?
I think there should be the way to transfer variable.
Posted: Tue Jul 24, 2007 8:30 am
by VladSun
So, you want to pass the arguments to the function by address, not by value - well, as far as I know you can't.
JS function arguments are passed by value. Sorry
"... And if I have the couple of variables?
I think there should be the way to transfer variable. ..."
You may return an object or an array.
Posted: Tue Jul 24, 2007 8:42 am
by Gente
Yeah I'm already doing my code with array. Just want to verify if transferring by address is possible.
Thank you all.