Can i send reference to a variable to a function?

Discuss any general programming issues here
Post Reply
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Can i send reference to a variable to a function?

Post by Bugala »

Here is what i am trying to achieve: (wrong example)

table.variable

function myfunc(func_variable)
func_variable = 1
endfunction


myfunc(table.variable)



What happens now is that when i access myfunc function, it is sending a value. A value that is on "table" at item "variable".
When i in function change that func_variable, it is simply changing the value of a independent copy of that value to 1.


What I would like to happen is, that when i change this value to 1, it would be affecting the value of the "table.variable" to become 1.

Therefore it seems to me like a good solution would be to send a reference of table.variable to that function, in which case it would indeed be changing table.variable to become 1, instead of independent variable,

But is it possible somehow to send a reference to a table item as one of the parameters?
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: Can i send reference to a variable to a function?

Post by Bugala »

Just pointing out that i do have a way around this problem, mainly sending both table and variable as separate info to the function and that way being able to do what i intend to, but it would be simpler if i would only need to send the reference to that variable inside a table.
User avatar
airsoftsoftwair
Posts: 5433
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Can i send reference to a variable to a function?

Post by airsoftsoftwair »

No, that's not possible. Just pass the table and modify the table member inside the function. Then it will automatically change in all table instances.
Post Reply