lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Bear in mind that all parameters passed by Lua are values.
If you assign something to a parameter, you change only the
value associated with the local variable, not the original.

a=1; b=2;
function f(a,b) b,a = a,b end
print(a,b) --> 1,2

The a,b of the function are not the global a,b.

If the value is a table, you get access to that table.
You can change the entries in the table, but you
cannot change the table itself.

> function f(set)
>         local newset,cloneset={},set
>         for x=1,#cloneset do
>                 newset[x]=cloneset[#cloneset]
>                 table.remove(cloneset)
>         end
>         return newset
> end

This function creates a new table containing
the elements of 'set' in reverse order, cleans out 'set',
and returns the new table.

> function
>
> g(set)
>         set=f(set)
> end

This function passes the value associated with 'set'
to 'f', which cleans it out and returns a table which
is assigned to the local variable 'set'. Thus 'set'
no longer refers to the original value, but to this
returned table. Since nothing further is done with
that table, it becomes garbage as soon as the
function returns. The original table remains as
empty as 'f' made it.