Lesson 22 of 55
9 mins readPython First-Class Functions & Callbacks
In Plain English
In Lua, functions are first-class values with no difference between a function name and a variable holding a function value. Pass functions as callbacks or higher-order arguments.
Deep Dive: How It Works
Anonymous Functions: `local double = function(x) return x * 2 end`.
Syntactic Sugar: `function foo() end` is exactly `foo = function() end`.
Higher-Order Functions: Functions that accept other functions or return them.
Core Rules to Remember

Functions are First-Class: Can be stored in variables, passed into tables, and passed as arguments.

Callback Paradigm: Standard foundation for event systems and custom sorting in Lua.
Live Interactive Example
Hit Run Code to see it liveHigher-Order Map Function
Python 3.12
1
2
3
4
5
6
7
8
9
10
11
12
Output Console
Click "Run Code" to view the rendered output.
How it works: map executed the anonymous lambda function for each element.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Apply Binary Operation Callback
Define `local function apply(a, b, op) return op(a, b) end`.
Call with an anonymous multiplication function and print `"Result: " .. apply(6, 7, function(x, y) return x * y end)`.
1
2
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.