HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to LuaVariablesNumbers & MathStringsTablesConditions (if)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Table
🔁 Condition & Loop
🧩 Function
Lua›Functions

Functions

Function definition, return, and multiple return values.

Simple enough for a 5-year-old to understand

function and return

Example code
1
function square(x)
Function definition.
FunctionFunction definition.
2
return x * x
Return the result.
FunctionReturn the result.
3
end
4
print(square(5))
25
Output / Input25
FunctionOutput / Input
💡 Hint: Functions open with function and close with end.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Write a function named addUp that adds two numbers; print addUp(8, 9).

🎯 Expected output: 17

Loading editor…

About this topic

In Lua, functions are first-class values: they can be assigned to a variable, passed as an argument to another function, and returned. This explains why callback-based game scripts are so easily written in Lua.

Functions can return multiple values. This is one of Lua's most useful features: you can get both the result of an operation and error information in a single call. The standard library uses this pattern heavily.

The number of arguments is flexible. Missing parameters become nil, extra ones are ignored — you won't get an error. This flexibility provides convenience but also hides typos, so checking parameters is a good habit.

What you will learn

  • Defining and calling a function
  • Returning multiple values
  • Missing and extra argument behavior
  • Using functions as values
  • Providing default values

Who is this for?

  • Those who want to organize their scripts
  • Game developers who will write callbacks
  • Those encountering argument errors

When to use?

Code that reacts to game events is written as functions and registered to the engine; this is the most common form of Lua usage.

If a piece of code is written a second time, it should be extracted into a function; as the script grows, this discipline becomes critical.

Common mistakes

Wrong

function topla(a, b)
  return a + b
end
print(topla(5))

Since the second argument is not provided, the value of b becomes nil and the addition throws the "attempt to perform arithmetic on a nil value" error. Lua does not consider a missing argument an error, it passes nil; the error only appears when that value is used. Therefore, the error message points inside the function rather than the call line.

Correct

function topla(a, b)
  b = b or 0
  return a + b
end
print(topla(5))

Wrong

local function hesapla()
  local a = 1
  local b = 2
end
print(hesapla())

Since the function returns nothing, nil is printed to the screen. In Lua, if return is not written, the function does not return a value; the value of the last expression does not return automatically. This is the opposite of what newcomers from some languages expect.

Correct

local function hesapla()
  local a = 1
  local b = 2
  return a + b
end
print(hesapla())

Wrong

print(selamla())
function selamla()
  return "Hello"
end

Since the function is called before it is defined, the name is nil at that moment and the "attempt to call a nil value" error is received. Lua executes the script from top to bottom; the function does not exist until the definition line is executed. Definitions need to be placed before calls.

Correct

function selamla()
  return "Hello"
end
print(selamla())

Frequently asked questions

How do I receive multiple values?

You write multiple variables separated by commas to the left of the call. If there are extra variables they get nil, if there are missing ones the remaining return values are discarded. If you only want the first one, wrapping the call in parentheses cuts off the rest.

How do I receive a variable number of arguments?

Three dots are used in the parameter list; all extra arguments are gathered there. You can find their count with the select function or pack them into a table with table.pack. It is frequently used in helper functions that generate daily error messages.

What is the difference between dot and colon calls?

The colon automatically passes the object itself as the first argument; it is used in object-oriented programming. If you call it with a dot, you need to provide that argument manually. Mixing the two is a classic source of errors in Lua.

Summary

Functions can be used as values.

Missing argument becomes nil, does not throw an error.

If return is not written, the function returns nil.

Multiple values can be returned.

The function must be defined before the call.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code