function who return 2 variable in one variable

hello i don’t understand how a variable can recover two variable

for example

function calcAvgCentre( points )

    local x, y = 0, 0   

    for i=1, #points do

        local pt = points[i]

        x = x + pt.x

        y = y + pt.y

    end   

    return { x = x / #points, y = y / #points }

end

local centre = calcAvgCentre( rect.dots )

[lua]

function calcAvgCentre( points )

    local tbl = {x= 0 ,y= 0 }

    for i= 1 , #points do

        local pt = points[i]

        tbl.x = tbl.x + pt.x

        tbl.y = tbl.y + pt.y

    end 

      tbl.x = tbl.x/#points

      tbl.y = tbl.y/#points

    return tbl

end

local centre = calcAvgCentre( rect.dots )

print (centre .x…" " …c entre.y)

[/lua]

Also watch out for the global function, unless you’ve pre-declared it as a local higher up in the code.

thanks 

lua supports multiple returns, so alternatively:

function calcAvgCentre( points ) local x, y = 0, 0 for i=1, #points do local pt = points[i] x = x + pt.x y = y + pt.y end return x / #points, y / #points end local x, y = calcAvgCentre( rect.dots )

In the original version that was posted at the top, its actually returning a single value, a table… ergo the { } around the values.  When it returns the table address is assigned to the variable centre, in effect creating a new table named centre with two members, .x and .y.  That example doesn’t return multiple values, it returns a table.

However everyone else is right.  Lua supports returning multiple individual values and their examples work as well.

Rob

[lua]

function calcAvgCentre( points )

    local tbl = {x= 0 ,y= 0 }

    for i= 1 , #points do

        local pt = points[i]

        tbl.x = tbl.x + pt.x

        tbl.y = tbl.y + pt.y

    end 

      tbl.x = tbl.x/#points

      tbl.y = tbl.y/#points

    return tbl

end

local centre = calcAvgCentre( rect.dots )

print (centre .x…" " …c entre.y)

[/lua]

Also watch out for the global function, unless you’ve pre-declared it as a local higher up in the code.

thanks 

lua supports multiple returns, so alternatively:

function calcAvgCentre( points ) local x, y = 0, 0 for i=1, #points do local pt = points[i] x = x + pt.x y = y + pt.y end return x / #points, y / #points end local x, y = calcAvgCentre( rect.dots )

In the original version that was posted at the top, its actually returning a single value, a table… ergo the { } around the values.  When it returns the table address is assigned to the variable centre, in effect creating a new table named centre with two members, .x and .y.  That example doesn’t return multiple values, it returns a table.

However everyone else is right.  Lua supports returning multiple individual values and their examples work as well.

Rob