lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 29/07/2013 14.43, Marius Booysen wrote:
Just starting with lua and I wanted to test multiple return values from
a function. I then ran into forward declaring issues (which I still am
not clear on how it works), so I ended up with this code:

Functions are first class objects in Lua (they are "values"), and variables are untyped (names don't have a type in Lua, values have) so
this line:

local func = function() end

is not a "function" declaration (as you might think), but it is a variable declaration ("func") with initalization, and you initialize it
with a function value.

That function value is "function() end", and that function doesn't return nothing, so when you execute it here

b, c = func()

b and c get that "nothingness" as nil values


print(b, c)

local function func()
     return 10, 11
end

This latter definition is not "the definition of function func", but a new definition for it, that shadows the previous one, but only for subsequent code.

When I execute is, it prints nil nil for the b and c.

Do all functions need to be complete before I can call it in Lua?

As I said there is no split (like in C/C++) between declaration and definition of functions in Lua. You declare names (variables), not values, and functions are values so they are not "declared", just defined with a suitable "function(--[[parameters here]]) ... end" expression.

Note that the alternative syntax (that looks more like a function definition of C):

local function func()
  -- code here
end

is syntactic sugar for

local func
func = function()
  -- code here
end


So pay attention because the line:

b, c = func()

does not call the function "func" (there is no "function func" technically speaking), but calls the object stored in the var named "func", which happens to be a function because you put a function object in func.

Note, moreover, that a function *definition* is an executable statement (unlike C/C++), so it creates a new function only if it is executed. There is no way in Lua to "call a function" whose definition has not been executed yet.

That's why most Lua script that doesn't use external modules you see lots of function definition at the beginning of the script and the "main" code is usually at the end, like this:

--------------------------------------------
local function f1()
 -- ....
end
local function f2()
 -- ....
end
local function f3()
 -- ....
end

-- here the code that calls f1, f2 and f3
--------------------------------------------

As a footnote, keep in mind that in Lua what you call "functions" are indeed something more complex, called "closures". If a closure has no upvalues (i.e. it doesn't refer to local variables defined in outer scopes), its behaviour is the same as that of a function as you may know it from other languages. But closures are much more powerful than simple functions, and their power is often used in many Lua idioms you will see when you improve your knowledge of Lua.


Thanks!

Cheers!

-- Lorenzo

--
()  ascii ribbon campaign - against html e-mail
/\  www.asciiribbon.org   - against proprietary attachments