Menu
Coddy logo textTech

Functions First-Class Values

Part of the Logic & Flow section of Coddy's Lua journey — lesson 12 of 54.

In Lua, functions are more than just blocks of code that perform tasks—they are values that can be treated like any other data type. This means you can work with functions in the same way you work with numbers, strings, or tables.

You can store a function in a variable:

local greet = function()
    print("Hello!")
end

greet()  -- Output: Hello!

Here, the function is assigned to the variable greet, and you can call it just like any regular function. This opens up powerful possibilities for how you structure your code.

Functions can also be passed as arguments to other functions. This is particularly useful when you want one function to use the behavior defined in another:

function executeAction(action)
    action()
end

local sayHello = function()
    print("Hello from a function!")
end

executeAction(sayHello)  -- Output: Hello from a function!

In this example, executeAction receives a function as its parameter and calls it. The function sayHello is passed in just like you would pass a number or string.

Functions can even return other functions. This allows you to create functions dynamically based on certain conditions or parameters:

function createMultiplier(factor)
    return function(number)
        return number * factor
    end
end

local double = createMultiplier(2)
print(double(5))  -- Output: 10
challenge icon

Challenge

Easy

Write a function applyOperation that takes a, b, and operation and returns the result of applying the operation function to the two numbers.

The operation parameter is a table containing a function that will be called with a and b as arguments.

Logic:

  • Extract the function from the operation table using the key "func"
  • Call that function with a and b as arguments
  • Return the result

Parameters:

  • a (number): First number
  • b (number): Second number
  • operation (table): A table containing a function stored at key "func"

Returns: The result of calling the operation function with a and b (number)

Cheat sheet

In Lua, functions are first-class values that can be stored in variables, passed as arguments, and returned from other functions.

Store a function in a variable:

local greet = function()
    print("Hello!")
end

greet()  -- Output: Hello!

Pass a function as an argument:

function executeAction(action)
    action()
end

local sayHello = function()
    print("Hello from a function!")
end

executeAction(sayHello)  -- Output: Hello from a function!

Return a function from another function:

function createMultiplier(factor)
    return function(number)
        return number * factor
    end
end

local double = createMultiplier(2)
print(double(5))  -- Output: 10

Try it yourself

function applyOperation(a, b, operation)
    -- Write code here
end
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow