Search code examples
lua

LUA local variable outside a function


This is my first attempt with LUA language, and I'm a little bit confused with variables declaration. I need to declare all variables as local, and as far I understand, local variable is only active on its scope (e.g. the function). What I need is pass a specific value to a function, "elaborate" it, and then assign another local function to be elaborated later, let's make a simple example:

local function test(x)
  local z=x
  return z
end

local x=1
test(x)

print(z)

So, 'x' is declared at main as local, then passed to the function. That function will just elaborate it and then assign the value to another local variable 'z'. The new variable will be used later outside the function. In the example I only assigned values, in the final code there will be much more operation on variables.

However the above print(z) will result in a nil value. I know that I can declare z as global, but unfortunately I have to keep local all variables because the script will be called by several thread, and 'x' can have different values while 'z' will be written as output.

Could someone help me to understand how to do that? Thank you, Lucas

EDIT: I can do something like:

local function test(x, y)
  local z=x+y
  local w=y-x
  return z,w
end

local z,w = test(1,2)

print(z)
print(w)

Is it a good approach? ...


Solution

  • As mentioned in comment and your own EDIT shows that too...
    The return of a function is important.
    And default values for the Arguments you can do...
    Guess!
    ...with local

    -- sol.lua
    
    local function sol(au, c)
    -- If au or/and c equals nil then using default in meter ( after or )
     local au, c = au or 149597870700, c or 299792458
    -- Than do all math in return
     return os.date('!%H:%M:%S', math.floor(au / c)), au, c
    end
    
    -- Defaults using meter
    -- sol() is interpreting it as sol(nil, nil)
    print(sol())
    -- Should put out: 00:08:19    149597870700    299792458
    
    -- au and c be averaged on kilometer
    print(sol(15e+7, 3e+5))
    -- Output: 00:08:20 150000000   300000
    
    -- only au be averaged on meter
    print(sol(15e+10))
    -- 00:08:20 150000000000    299792458
    
    -- Only c be averaged on meter
    -- All notexistent or undefined variable names can be used for nil as well
    -- An exception: false will fallback to default too
    -- true will fail with: attempt to perform arithmetic on local 'au' (a boolean value)
    print(sol(nil, 3e+8))
    -- 00:08:18 149597870700    300000000
    
    -- Check out whats wanted
    -- Nearly the same like your EDIT approach
    local _, au, c = sol()
    print(string.format('Astronomical Unit => %d Meter\nSpeed of Light => %d Meter/Second', au, c))
    -- Astronomical Unit => 149597870700 Meter
    -- Speed of Light => 299792458 Meter/Second
    
    -- Direct access
    print(({sol()})[3])
    -- 299792458
    print(({sol()})[2])
    -- 149597870700
    print(({sol()})[1])
    -- 00:08:19
    
    -- Next makes sol.lua requirable
    return sol
    -- Example: sol = require('sol')
    

    ;-)