Posted By: TripleX
Call by Reference in C-Script - 11/06/05 13:13
Hey
As you know you can not easily change Variables passed to a function as parameter, in this function.
Example:
Code:
This won't work. Why? Because we work with a copy of the orginal value not with the variable itself.
So we have to pass the pointer of the variable to the function? How does this work? Exactly as if you would pass a vector to a function. With the "&" operator. What does that mean? You always pass the pointer of a vector to a function! That's why the same code as above with pointers would be possible.
A code example:
Code:
I posted that because this info is really nowhere mentioned in any tutorial..Via & you don't HAVE to pass a vector! You say the compiler that he has to expect a memory location as parameter.
Triple-X
PS: That is also the reason why in a DLL you expect a pointer to a vector but the value of a var ( DLLFUNC var tst(var b); DLLFUNC var tst(VECTOR* b); )
As you know you can not easily change Variables passed to a function as parameter, in this function.
Example:
Code:
function test(a_);
function main()
{
var c = 0;
test(c);
}
function test(a_)
{
a_ = 50;
}
This won't work. Why? Because we work with a copy of the orginal value not with the variable itself.
So we have to pass the pointer of the variable to the function? How does this work? Exactly as if you would pass a vector to a function. With the "&" operator. What does that mean? You always pass the pointer of a vector to a function! That's why the same code as above with pointers would be possible.
A code example:
Code:
function test(&a_); //pass the memory location, not a copy of the var
function main()
{
var c = 0;
test(c);
}
function test(&a_)
{
a_[0] = 50; //c will have the value 50
}
I posted that because this info is really nowhere mentioned in any tutorial..Via & you don't HAVE to pass a vector! You say the compiler that he has to expect a memory location as parameter.
Triple-X
PS: That is also the reason why in a DLL you expect a pointer to a vector but the value of a var ( DLLFUNC var tst(var b); DLLFUNC var tst(VECTOR* b); )