Hi,
Silly question… how do you pass a variable by ref to a function in lua
ie
[code]
local a = 1
function test(a)
a = a + 1
end
test(a)
print(a)
[code]
I want ‘a’ to print 2… [import]uid: 67619 topic_id: 15690 reply_id: 315690[/import]
Hi,
Silly question… how do you pass a variable by ref to a function in lua
ie
[code]
local a = 1
function test(a)
a = a + 1
end
test(a)
print(a)
[code]
I want ‘a’ to print 2… [import]uid: 67619 topic_id: 15690 reply_id: 315690[/import]
primitives (ints, strings and booleans) are passed by value only, where as tables are passed by reference.
The reason that other languages used reference or value was in a way to return multiple values, Lua can anyways return multiple values, so if you had code like
[lua] local a = 2
local b = 3
local c = 4
function doubleit(a,b)
return a*2, b*2
end
a,b = doubleit(a,b)
a,c = doubleit(a,c)
b,c = doubleit(b,c)
print("a = ", a)
print("b = ", b)
print("c = ", c)[/lua]
You will get the values as 8,12 and 16 respectively
LUA requires you to think a bit differently, and it can be amazing!!
cheers,
?
[import]uid: 3826 topic_id: 15690 reply_id: 57909[/import]
Thanks mate… I really appreciate the help [import]uid: 67619 topic_id: 15690 reply_id: 58287[/import]