0Pricing
Lua Academy · Lesson

Functions as Values

Store and pass functions.

Functions as Values is a free Lua Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Functions Are First-Class

In Lua, functions are first-class values. That means a function is just data: you can store it in a variable, pass it to another function, return it, and keep it in a table.

This single idea powers all of functional programming. Once you treat functions like numbers or strings, you can build flexible, reusable tools.

Storing a Function in a Variable

The name you give a function is really just a variable holding the function value. You can assign that value to another variable freely.

Both names below point to the same underlying function, so calling either one runs the same code.

local function greet(name)
  return "Hello, " .. name
end

local hi = greet
print(greet("Ada"))
print(hi("Lin"))

Anonymous Functions

You do not need a name to create a function. An anonymous function is written with the function ... end syntax and used directly as a value.

Assigning one to a local is the same as a normal function definition, just written inline.

local square = function(x)
  return x * x
end

print(square(5))
print(square(9))

Functions as Arguments

Because functions are values, you can pass them into other functions. A function that takes another function is called a higher-order function.

Here apply does not know what work to do until you hand it a function to run.

local function apply(f, x)
  return f(x)
end

local function double(n) return n * 2 end

print(apply(double, 10))
print(apply(function(n) return n + 1 end, 10))

Returning a Function

A function can return another function. This lets you build functions on demand, configured by the arguments you passed.

Below, multiplier builds a brand-new function each time, remembering the factor you gave it.

local function multiplier(factor)
  return function(x)
    return x * factor
  end
end

local triple = multiplier(3)
print(triple(4))
print(multiplier(10)(5))

Closures Capture Variables

When a returned function uses a local from the enclosing function, it captures that variable. The pair of function plus captured variables is called a closure.

The captured factor stays alive as long as the closure does, even after the outer call has finished.

local function counter()
  local n = 0
  return function()
    n = n + 1
    return n
  end
end

local next = counter()
print(next())
print(next())
print(next())

Functions in Tables

Tables can hold functions just like any other value. This is how Lua builds modules and objects: a table maps names to function values.

You can look up a function by key and call it, or even iterate over a table of operations.

local ops = {
  add = function(a, b) return a + b end,
  sub = function(a, b) return a - b end
}

print(ops.add(2, 3))
print(ops["sub"](9, 4))

A Table of Strategies

Storing functions in a table lets you pick behavior at runtime by key. This replaces long if-else chains with a clean lookup.

Choosing the operation by name keeps the calling code short and easy to extend with new keys.

local dispatch = {
  upper = string.upper,
  lower = string.lower,
  rev = string.reverse
}

local function run(name, s)
  return dispatch[name](s)
end

print(run("upper", "lua"))
print(run("rev", "lua"))

Callbacks

A callback is a function you pass so it can be called later. Lua's standard library uses this pattern, for example table.sort takes a comparison function.

The callback decides the rule; the library handles the looping and rearranging.

local nums = {5, 2, 9, 1}
table.sort(nums, function(a, b)
  return a > b
end)

print(table.concat(nums, ", "))

Functions Have Identity

Every function value is a distinct object. Two separate function literals are never equal, even if they contain the same code.

But a single function stored in two variables compares equal, because both names reference the same value.

local f = function() return 1 end
local g = function() return 1 end
local h = f

print(f == g)
print(f == h)

Why This Matters

Treating functions as values lets you write generic tools once and customize them with the functions you pass in. Mapping, filtering, partial application, and composition all rely on this.

The rest of this course builds practical patterns on top of first-class functions and closures.

Quick Check

Test your understanding of first-class functions in Lua.

Recap

Functions in Lua are first-class values: store them in variables and tables, pass them as arguments, and return them from other functions.

Returning a function that captures enclosing locals creates a closure. These ideas are the foundation for every functional pattern ahead.

Frequently asked questions

Is the “Functions as Values” lesson free?

Yes — the full text of “Functions as Values” is free to read here on the web, and the Lua Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Lua Academy course, upgrade to CoddyKit PRO.

What will I learn in “Functions as Values”?

Store and pass functions. You practise Lua Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Lua Academy?

No prior experience is required. Lua Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Functions as Values” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Lua Academy lesson?

Yes. Every Lua Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Functions as Values
  2. map, filter, reduce
  3. Partial Application
  4. Composing Functions
← Back to Lua Academy