How to properly write a function

Hi all I have been fiddling a bit with Corona already but I want to get into the habit of best practices. I also looked at a ton of templates and tutorials.

I’m trying to see if there is a distinction in how code is read when a function is written in each of the following ways and if there isn’t is one better than the other or just a preference?

example 1 local function openDoor(event) -- Do something end example 2 local openDoor = function(event) -- Do something end

is example 1 = example 2 or do they work differently from a structural standpoint?

Thank you

Hi Pickion,

I personally always use the structure you outlined in “example 1” and here’s why: when you construct your functions that way, you can refer to those functions, from within those functions. For example:

local n = 1 local function openDoor(event) if n\<=5 then print("opened Door "..n.." times.") n = n+1 openDoor() else print("done opening door.") end end openDoor()

If you try to run that same function, but declare it using the structure local openDoor = function(event), it will not work. To the best of my knowledge, that’s the only real advantage of using one method over the other, but since there is no downside to using the method outlined above, it’s probably just a good habit to get into. 

Here’s some more explanation on this subject, taken from http://lua-users.org/wiki/ScopeTutorial :

local function f() end -- is equivalent to local f f = function() end -- not local f = function() end the difference between the last two examples is important: the local variable still doesn't exist to the right of the = that gives it the initial value. So if the contents of the function used f to get a reference to itself, it will correctly get the local variable in the first and second versions, but the third version will get the global f (which will be nil, if not a completely unrelated value set by some other code).

Hope that helps. P.S. I saw you followed me on Twitter today - thanks! :slight_smile:

Hi Schroederapps

Thanks for the detailed explanation.

I ran the code example and it worked, and as expected when I switched just the top line to example 2 style it gave an error, I will keep using the example 1 format as it appears to be working well and has extra benefits too.

I’m always happy to follow members in the community.

Hi Pickion,

I personally always use the structure you outlined in “example 1” and here’s why: when you construct your functions that way, you can refer to those functions, from within those functions. For example:

local n = 1 local function openDoor(event) if n\<=5 then print("opened Door "..n.." times.") n = n+1 openDoor() else print("done opening door.") end end openDoor()

If you try to run that same function, but declare it using the structure local openDoor = function(event), it will not work. To the best of my knowledge, that’s the only real advantage of using one method over the other, but since there is no downside to using the method outlined above, it’s probably just a good habit to get into. 

Here’s some more explanation on this subject, taken from http://lua-users.org/wiki/ScopeTutorial :

local function f() end -- is equivalent to local f f = function() end -- not local f = function() end the difference between the last two examples is important: the local variable still doesn't exist to the right of the = that gives it the initial value. So if the contents of the function used f to get a reference to itself, it will correctly get the local variable in the first and second versions, but the third version will get the global f (which will be nil, if not a completely unrelated value set by some other code).

Hope that helps. P.S. I saw you followed me on Twitter today - thanks! :slight_smile:

Hi Schroederapps

Thanks for the detailed explanation.

I ran the code example and it worked, and as expected when I switched just the top line to example 2 style it gave an error, I will keep using the example 1 format as it appears to be working well and has extra benefits too.

I’m always happy to follow members in the community.