Back to Course HomeLesson 7 of 19
Intermediate • Part 1

Functions in Lua: The Building Blocks of Programs

Lesson Overview

Functions are one of the most powerful features in Lua. They let you organize code, reduce repetition, and abstract complexity. Nearly every Lua program uses functions extensively.

This lesson provides a deep, comprehensive exploration of functions in Lua—from the basics to advanced concepts like closures and higher-order functions.

Lesson Objectives

By the end of this lesson, you will be able to:

  • Define and call functions
  • Use parameters and return values
  • Understand local vs global functions
  • Use multiple return values
  • Apply variable arguments (varargs)

What Are Functions?

A function is a named block of code that performs a specific task. You can call it whenever you need that task performed.

Functions allow you to:

  • • Reuse code without repetition
  • • Organize complex logic into manageable pieces
  • • Test and debug smaller units of code
  • • Create modular, maintainable programs

Defining a Function

Basic Syntax

function functionName()
    -- code to execute
end

Example

LUA
function greet()
    print("Hello, World!")
end

greet() -- Outputs: Hello, World!

Function Parameters

Parameters let you pass data into functions.

LUA
function greet(name)
    print("Hello, " .. name .. "!")
end

greet("Alice") -- Outputs: Hello, Alice!
greet("Bob")   -- Outputs: Hello, Bob!

Multiple Parameters

LUA
function add(a, b)
    print(a + b)
end

add(5, 3)  -- Outputs: 8
add(10, 20) -- Outputs: 30

Return Values

Functions can return values using the return keyword.

LUA
function add(a, b)
    return a + b
end

local result = add(5, 3)
print(result) -- Outputs: 8
Key Point: A function stops executing when it hits return.

Multiple Return Values

Lua functions can return multiple values at once!

LUA
function getMinMax(a, b)
    if a < b then
        return a, b
    else
        return b, a
    end
end

local min, max = getMinMax(10, 5)
print(min, max) -- Outputs: 5  10

You can ignore return values by not assigning them.

Local vs Global Functions

By default, functions are global. Use local to limit scope.

Global Function

function globalFunc()
    print("I'm global")
end

Local Function

local function localFunc()
    print("I'm local")
end
Best Practice: Always use local functions unless you explicitly need global access.

Anonymous Functions

Functions don't need names! They can be assigned to variables or passed as arguments.

LUA
local greet = function(name)
    print("Hello, " .. name)
end

greet("World") -- Outputs: Hello, World

Variable Arguments (Varargs)

Use ... to accept any number of arguments.

LUA
function sum(...)
    local total = 0
    for _, v in ipairs({...}) do
        total = total + v
    end
    return total
end

print(sum(1, 2, 3, 4, 5)) -- Outputs: 15

Functions as First-Class Values

In Lua, functions are first-class values. This means:

  • • They can be stored in variables
  • • They can be passed as arguments
  • • They can be returned from other functions
  • • They can be stored in tables
LUA
-- Storing in a table
local operations = {
    add = function(a, b) return a + b end,
    sub = function(a, b) return a - b end
}

print(operations.add(5, 3)) -- Outputs: 8

Common Function Mistakes

  • Forgetting to call the function with ()
  • Not handling nil parameters
  • Ignoring return values
  • Creating global functions accidentally
  • Forgetting end keyword

Ready to test your knowledge?

Take the quiz to verify your understanding of functions.