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 6999861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:35:47+00:00 2026-05-27T20:35:47+00:00

What is the difference between the following Lua scripts in terms of function scope.

  • 0

What is the difference between the following Lua scripts in terms of function scope. How would it effect the need to require 'calculator' in some other Lua script. And how would it be referenced in say a LuaState.getGlobal(function_name). What would be its proper function name? Also any comment on advantages/disadvantages of the declaration approaches.

A) Calculator.lua

function foo(n) 
   return n+1;
end

B) Calculator.lua

calc= {}
function calc.foo(n) 
  return n+1;
end

C) Calculator.lua

function foo(n) 
   return n+1;
end
function calculator()
   calc = {}
   calc.foo=foo
   return calc
end
  • 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-27T20:35:48+00:00Added an answer on May 27, 2026 at 8:35 pm

    I don’t know what it is you mean exactly by the “scope” of these scripts. Exactly what these scripts do depends on how they’re being executed. You could give them a different environment, thus changing what they think of as “globals”.

    So, I will explain what each of these scripts does based on the assumption that you are loading them with dofile, dostring, or something of the like. That is, you’re applying them to the global environment.

    A)

    This creates a single global variable, foo, which is a function.

    B)

    This creates a single global variable, calc, which is a table. This table has a single entry, with the key foo, who’s value is a function.

    C)

    This creates two global variables. foo is a function. calculator is also a function. Each call to calculator will cause the global variable calc to be overwritten. The new value of calc will be a table that has a single entry, with the key foo, who’s value is a copy of what is stored in the global variable foo.

    It’s hard to say what the “advantages” of method C are, since it makes no sense. It creates two functions instead of one, and that second function keeps creating new tables.

    B is just a “namespace” scoped version of A. The general expectation with Lua modules is that including them will usually create some table that will contain all of the functions in that module, to avoid name conflicts with existing global state. In that regard, B may be better. But it depends mostly on what this script will be used for.

    Personally, for simple shell scripts and utility scripts, I don’t bother with proper module scoping. When I do make a proper module, I generally use the module Lua function.

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

Sidebar

Related Questions

What is the difference between the following two declarations? Class.method = function () {
What is the difference between the following 2 examples of code: (function(){ var myFunc
what is the difference between following function declarations, which create and return the array
What is the difference between the following concepts in layman terms: spinning lock, blocking
Please highlight the difference between the following function declarations: void (*p) (void *a[], int
What is the difference between the following function definitions? 1: $(function () { //stuff
What is difference between following code? Code 1: var f = function() { //
Is there a difference between the following two? msgbox() messagebox.show() Some tutorials use msgbox(),
Is there any difference between following two ways of creating an object. Student s1
Question 1 Can anyone tell me if there is any difference between following 2

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.