Passing by reference/value

Posted By: Chameleon_Man

Passing by reference/value - 12/08/07 17:47

In c-script, how do I pass a var by reference and value? What about making a function return a value? Thanks.
Posted By: Fenriswolf

Re: Passing by reference/value - 12/08/07 18:22

Hi,

Quote:

how do I pass a var by reference and value?



By value: function func(my_var) {...}
By reference: function func(&my_var) {...}

By using '&' you can also pass arrays as parameters.

Quote:

What about making a function return a value?




function func(my_var) {
return(my_var*2);
}

In C-Script you can return vars only.
However, as vars can also be used to save pointers you can return pointers to entities, strings a.s.o. as well.
Posted By: Chameleon_Man

Re: Passing by reference/value - 12/08/07 20:30

Error(0): Syntax error - local var can't be passed as vector.

I get this error when I try to pass as reference. My code looks a little like this...

Function Main {
var momentum=0;
jump(momentum);
}

Function jump(&momentum) {
...
}
Posted By: EpsiloN

Re: Passing by reference/value - 12/09/07 04:46

Local vars are vars that have been created insinde the function.
Move 'var momentun=0;' before your function main() and it'll work. (This way you'll make it a global var)
Posted By: Chameleon_Man

Re: Passing by reference/value - 12/09/07 06:16

That's a quick fix to the problem, but I'd like to avoid using global variables. Too many globals are a sign of bad programming...
Posted By: EpsiloN

Re: Passing by reference/value - 12/09/07 07:45

Quote:

Too many globals are a sign of bad programming...



Ok , so we're all bad programmers
If you sit down and think about it for a minute , you have one 'you' pointer wich is global and is used by 100s of functions at the same time. How ? I'll leave that up to you , because a sign of bad programming is exactly that , not knowing how you could use one global var/pointer in many diffrent functions between two frames.
© 2024 lite-C Forums