Function differences?

I am playing with memory management from this link:
http://www.coronalabs.com/blog/2011/08/15/corona-sdk-memory-leak-prevention-101/

And I notice this function is being called:

local monitorMem = function()  
 collectgarbage()  
 print( "MemUsage: " .. collectgarbage("count") )  
 local textMem = system.getInfo( "textureMemoryUsed" ) / 1000000  
 print( "TexMem: " .. textMem )  
end  
  
Runtime:addEventListener( "enterFrame", monitorMem )  

I’m getting confused between the code above: local monitorMem = function()…
And perhaps a standard way to write a function: local function monitorMem()…

Are these 2 ways synonymous? Or is there a distinct difference?

[import]uid: 154122 topic_id: 28230 reply_id: 328230[/import]

Yes the second is syntactic sugar for the first, with the exception that defining a function using the first method can’t be called recursively.

As Pil defines it:

A subtle point arises in the definition of recursive local functions. The naive approach does not work here:

local fact = function (n) if n == 0 then return 1 else return n\*fact(n-1) -- buggy end end
When Lua compiles the call fact(n-1), in the function body, the local fact is not yet defined. Therefore, that expression calls a global fact, not the local one. To solve that problem, we must first define the local variable and then define the function:

local fact fact = function (n) if n == 0 then return 1 else return n\*fact(n-1) end end [import]uid: 33275 topic_id: 28230 reply_id: 114026[/import]

When you write:

local function bubba()  

Lua creates a variable called “bubba” and stores the address of the function code in that variable so when you write:

bubba()  

the program will branch to that function and execute it.

When you see:

local bubba = function()  

You are creating an anonymous (unnamed) function and assigning it’s address to the variable “bubba”. In effect you’ve done the exact same thing as above. Two way’s to skin a cat, so to speak.

But where it comes important is a term called “Forward declaration”. In most compiled languages like C, there are “two passes”, the first one builds a large table of all the “symbols” used (i.e variables, function names etc.), then the second pass deals with actually spewing code.

Corona SDK is more of a one pass process and you can’t reference a variable or function before you define it.

Lets look at this scenerio, you want to fade in and out an object:

  
local function fadeIn(target)  
 transition.to(target,{time=500, alpha=1.0, onComplete=fadeOut})  
end  
  
local function fadeOut(target)  
 transition.to(target,{time=500, alpha=0.0, onComplete=fadeIn})  
end  
  
fadeout()  

This will fail in Lua because when the compiler starts looking at the function fadeIn() it tries to reference “fadeOut” which hasn’t been defined yet.

The solution to this problem is to use forward declaration, which means using the second form of the function declaration.

local fadeOut  
  
local function fadeIn(target)  
 transition.to(target,{time=500, alpha=1.0, onComplete=fadeOut})  
end  
  
fadeOut = function(target)  
 transition.to(target,{time=500, alpha=0.0, onComplete=fadeIn})  
end  
  
fadeout()  

Now we have declared our intent to use the variable fadeOut early so the compiler knows about it, and later we actually put the function reference into that variable once we know about it. [import]uid: 19626 topic_id: 28230 reply_id: 114072[/import]