This tutorial discusses the basics behind anonymous functions and closures in Lua. Let’s begin by inspecting how a typical Lua function looks:

Under the hood, this creates a variable called myFunction that is scoped locally to this code block and is stored where the code block exists in memory. For instance if you did…

…the Terminal/console would print something like this:


This is the location in memory where Lua will jump when the function is called.

Lua also permits you to define a function like this:

In this notation, it’s more clear that you’re assigning a variable to a code block (or more specifically the memory address of that code block). In the end, both notations accomplish the same thing, but the second notation allows some additional flexibility. When written in this form, you are effectively taking an unnamed block of code and giving it a name.

Lua permits you to write these functions and use them without a name.  This is called an anonymous function.

Anonymous Functions

To explore anonymous functions, let’s look at some examples :

In this case, the timer.performWithDelay() function takes a time interval in milliseconds, a function to execute, and an integer count to repeat. Thus, myFunction() will fire 10 times on every 1000 millisecond interval, but what’s really happening is that Lua is passing the memory address of the function to the timer.performWithDelay() function. This means that you could also write…

…or this (note the semicolon after the print() command…

In both cases, instead of using a variable to hold the function’s memory address, you simply create an unnamed anonymous function that will execute when the timer fires. Anonymous functions can be used anywhere you need to code an onComplete or callback listener function, for example in,, etc., but how often should you use them? From the perspective of code readability and sustainability, perhaps not very often — they tend to make your code harder to read, but if the function will only be used in that specific scope of your code, and it’s relatively short, anonymous functions are a valid coding technique.

In the second example, notice the semicolon at the end of line 3. If you write just one statement of code, for example one print() statement, the semicolon isn’t required, but if you write multiple statements on one line, they must be separated by semicolons. Therefore, it’s a good habit to include the semicolon, even if you just include one line of code within the anonymous function.

Be cautious: there are times when you can not use anonymous functions. One specific instance is when you assign functions to event listeners. Because the event listener system in Corona is based around storing the address of functions that are specified to handle events, the address that you pass in when removing an event listener via object:removeEventListener() must match the address that was passed in to object:addEventListener().

Lua Closures

Lua closures are another useful technique. Where they truly shine is in passing parameters to function handlers that are not looking for extra parameters. To illustrate this, let’s look at a example:

In this example,  myFunction() will be passed a single parameter, target, which is the object that is transitioning. But let’s assume that the display object is a member of on array and you need to know more about the object itself in the function that gets called. This can be accomplished via a Lua closure:

In this example, instead of calling myFunction() as part of the callback, you call an anonymous function instead. That anonymous function takes the received target parameter that provides and, inside it, you call your named function, passing along the target object reference and the color property of the ball.

Closures are more than just anonymous functions. When you have a function inside of a function, the child function has access to all of its parent’s local objects, or upvalues. Normally, when a function ends, any locally-scoped variables go out of scope, so when you call the function again, you begin anew. Consider this example:

Each time that you call counter(), it will print the value of 10 because when the function’s scope ends, so do its local variables.

Now let’s modify the function to actually return a function that does the counting:

At this point, we have a new function in our counter1 variable, and when we run it multiple times…

…Lua realizes that the anonymous function may still get used and that it should hold on to the current value of i in its current scope. This is what a Lua closure does for you.

To expand on this concept, if you were to create another counter, the following results would occur:

Essentially, each version of the anonymous function holds its own i and keeps it scoped within itself. So, when you create a second counter, i resets to 0 for that instance.

In Summary

As you can see, anonymous functions and closures provide a powerful way to keep track of variable instances on a per-function basis. In the context of Corona SDK, this allows you to pass parameters to functions in which you normally don’t control the passed-in content.

To continue your exploration of these topics, please continue to the Lua documentation site.

Taken from:  

Tutorial: Anonymous Functions and Closures