Rob Cook


Home | GitHub | Dev.to | Donate | Contact

[13] A little bit of Lua


2026-01-23

Something we haven't looked at yet is the interaction between functions and tables. Lets do that now.

Operations on shapes

Assume we are writing a Lua program to perform operations on shapes. We want to calculate the area and perimeter of a circle and a rectangle. We could name the the area functions circle_area and rectangle_area, and the perimeter ones circle_perimeter and rectangle_perimeter. However, this approach soon gets a bit cluttered and error prone. It would be nice if we could group the related methods together in some form of structure that provides a common prefix. As it happens, Lua tables can be used for just this purpose.

Defining functions on a table

Take a look at the following Lua program. In it we define two tables circle and rectangle. We then define an area and perimeter function on each table by using the table name and a dot . in the function declaration. We can then call the different functions using the same dotted name notation.

-- Functions and tables.

local circle = {}
local rectangle = {}

function circle.area(radius)
  return math.pi * (radius ^ 2)
end

function circle.perimeter(radius)
  return 2 * math.pi * radius
end

function rectangle.area(length, width)
  return length * width
end

function rectangle.perimeter(length, width)
  return (length * 2) + (width * 2)
end

io.write("Area of a circle with radius 2.5 is " .. circle.area(2.5) .. "\n")
io.write("Area of a rectangle with dimensions 4 x 3 is " .. rectangle.area(4, 3) .. "\n")
io.write("Perimeter of a rectangle with dimensions 4 x 3 is " .. rectangle.perimeter(4, 3) .. "\n")

A table that contains a set of functions in this manner is sometimes referred to as a namespace. Namespaces are a great way to group related code together, and to avoid clashes that would otherwise happen with names.

Passing tables to functions

Lets assume we have a function that takes a large number of arguments. As Lua's arguments are assigned to parameters positionally, it can be tricky to remember the right order for things.

local function big_func(width, height, colour, thickness, fill)
  io.write(width .. " x " .. height)
  -- Do stuff with other parameters...
end

big_func(4, 5, "red", 0.1, true)

Instead of taking a list of parameters we can define the function to take a single table instead. When calling it we use a special notation that drops the need for parenthesis.

local function big_func(params_table)
  io.write(params_table.width .. " x " .. params_table.height)
  -- Do stuff with other parameters in the table...
end

big_func{ width = 4, fill = true, thickness = 0.1, colour = "red", height = 5 }

Notice how we can mix up the order of arguments in the table. This is because we are assigning them to keys in the table. These can then be read out in the function just as we would normally read a value from a table by key.

When you have a function that needs to take a large set of values as arguments, consider replacing them with a single table and invoke it using the special syntax. This can aid readability of your code.

end