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.
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.