Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6807501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:50:24+00:00 2026-05-26T19:50:24+00:00

I am learning Lua and have come across the concept of anonymous functions. It’s

  • 0

I am learning Lua and have come across the concept of anonymous functions. It’s interesting but I was wondering what additional advantage it provides over non anonymous functions.

So If I have something like

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

The function is anonymous and if I have

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

The function is non anonymous. The second is better because I can call it wherever I want and I also know what my function is doing. So what’s the advantage of anonymous functions? Am I missing something here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:50:25+00:00Added an answer on May 26, 2026 at 7:50 pm

    To be honest, there is no such thing as a named function in Lua. All functions are actually anonymous, but can be stored in variables (which have a name).

    The named function syntax function add(a,b) return a+b end is actually a syntactic sugar for add = function(a,b) return a+b end.

    Functions are often used as event handlers and for decisions which a library does not/cannot know, the most famous example being table.sort() – using your function, you can specify the sorting order:

    people = {{name="John", age=20}, {name="Ann", age=25}}
    table.sort(people, function (a,b) return a.name < b.name end)
    

    The point is that most probably you won’t need the function later. Of course, you can also save the function to a (possibly local) variable and use that:

    local nameComparator = function (a,b) return a.name < b.name end
    table.sort(people, nameComparator)
    

    For more information, read this section on functions in PiL.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I come from classes object orientation languages and recently I have been learning those
learning Jquery and integrating with PHP - getting there, but have one last challenge
I was looking into learning Lua because it has some interesting features (speed being
Learning javascript when I came across the concept of prototype. I succeeded in adding
Learning a little about T-SQL, and thought an interesting exercise would be to generate
Im learning lisp and im pretty new at this so i was wondering... if
I'm currently learning lua. regarding pattern-matching in lua I found the following sentence in
Learning jquery, so please be kind :) Using PHP, I have a table with
Learning Ruby. Needed to create a hash of arrays. This works... but I don't
I'm in the design/skeleton coding phase of my C++ game with Lua scripting, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.