functions, events, and variables

in simple words what would the difference be from

function variableName(event)

and

local variableName = function(event)

thanks for your help :slight_smile:

In lua there is no difference because functions are stored in variables as pointers to space in memory where they reside. So when you do variableName() what realy happens is you telling lua to execude piece of code residing in part of memory on which points variableName. First one creates function under ‘variableName’ variable in one go. Second one breaks it into 2 steps. First create function anx when created it is assigned to variable (you can read initialization vs declaration). Nevertheless, outcome is the same

thanks for the reply

is there a benefit to either one?

is the event inside the parenthisis always calling for an event listener?

and is the function without the local make it automatically public like other variables?

Benefit? Probably not, but even if some, it’s probably totaly insignificant.

Usually argument for corona listener is table - you can call it event, e, tab - as you like. However there is also such concept as ‘table listener’ which will take one more parameter - but this if you are interested.

Everything in lua which isn’t local automatically becomes global - even funtions because they have the same rights as variables

thanks for you post and time, and for catching my error, i said public and i meant global, so smart :slight_smile:

The format:

local function somename(someparams)

method is similar syntax to other languages which is more comfortable to programmers coming from other languages.  

local somename

 

somename = function(someparams)

is helpful when you need to access a function before you define the code for it.  There is not much of a difference beyond that.   Now as for the “event” parameter, it’s just a parameter.  It could be called fred if you wanted it to be.   People tend to name parameters for what their purpose is.  And for functions that are called from events that triggered, it’s logical to name the table of data about that event be named “event”.  Some people shorten it to “e”, but over all spelled out names and consistent use of names helps code readability.

Functions without a local can be seen as “public” in an object oriented definition, but they are also “global” in a scope perspective and global’s are not the best way to do things.   You will see things like:

function someobject.somefunction(someparameter)   or

function someobject:somefunction(someparameter)

This is more of the object oriented way of declaring a public function.  In this case “someobject” was declared locally earlier as a table.  You are adding methods to the object.  The second form is the same as writing:

function someobject.somefunction( self , someparameter)

Where self is a reference to the object itself.  Kind of an OOP concept.  Lua allows either way.

Right

local func

func = function ()
end

Will allow you to write some code in ‘…’ which can call func() before it’s content is written (forward declaration) - but this part in ‘…’ must be called after you assign content (function) to func variable

In lua there is no difference because functions are stored in variables as pointers to space in memory where they reside. So when you do variableName() what realy happens is you telling lua to execude piece of code residing in part of memory on which points variableName. First one creates function under ‘variableName’ variable in one go. Second one breaks it into 2 steps. First create function anx when created it is assigned to variable (you can read initialization vs declaration). Nevertheless, outcome is the same

thanks for the reply

is there a benefit to either one?

is the event inside the parenthisis always calling for an event listener?

and is the function without the local make it automatically public like other variables?

Benefit? Probably not, but even if some, it’s probably totaly insignificant.

Usually argument for corona listener is table - you can call it event, e, tab - as you like. However there is also such concept as ‘table listener’ which will take one more parameter - but this if you are interested.

Everything in lua which isn’t local automatically becomes global - even funtions because they have the same rights as variables

thanks for you post and time, and for catching my error, i said public and i meant global, so smart :slight_smile:

The format:

local function somename(someparams)

method is similar syntax to other languages which is more comfortable to programmers coming from other languages.  

local somename

 

somename = function(someparams)

is helpful when you need to access a function before you define the code for it.  There is not much of a difference beyond that.   Now as for the “event” parameter, it’s just a parameter.  It could be called fred if you wanted it to be.   People tend to name parameters for what their purpose is.  And for functions that are called from events that triggered, it’s logical to name the table of data about that event be named “event”.  Some people shorten it to “e”, but over all spelled out names and consistent use of names helps code readability.

Functions without a local can be seen as “public” in an object oriented definition, but they are also “global” in a scope perspective and global’s are not the best way to do things.   You will see things like:

function someobject.somefunction(someparameter)   or

function someobject:somefunction(someparameter)

This is more of the object oriented way of declaring a public function.  In this case “someobject” was declared locally earlier as a table.  You are adding methods to the object.  The second form is the same as writing:

function someobject.somefunction( self , someparameter)

Where self is a reference to the object itself.  Kind of an OOP concept.  Lua allows either way.

Right

local func

func = function ()
end

Will allow you to write some code in ‘…’ which can call func() before it’s content is written (forward declaration) - but this part in ‘…’ must be called after you assign content (function) to func variable