local function vs non local function

I’m not entirely clear what the difference is.
But after testing, I came up with a conclusion of when to use “local” before a function.

Here’s an example:

local isAudioPlaying = false;  
local objSnd = audio.loadSound("myfilepath.mp3");  
  
local function runSoundFunction(e)  
 if(e.phase=="began" and isAudioPlaying==false) then  
 isAudioPlaying = true;  
 audio.play(objSnd, {onComplete=donePlaying});  
 end   
end  
  
--do not put "local" before this function  
function donePlaying(e)  
 isAudioPlaying = false;  
end  
  
Runtime:addEventListener("touch", runSoundFunction)  

I used to have “local” before function donePlaying and it wasn’t detected inside the runSoundFunction
Why is that?

In retrospect, why do when do we need to use local for variables? And when is best not to?

[import]uid: 154122 topic_id: 27320 reply_id: 327320[/import]

or…

local function runSoundFunction(e)  
 if(e.phase=="began" and isAudioPlaying==false) then  
 isAudioPlaying = true;  
 audio.play(objSnd, {onComplete=function() isAudioPlaying = false end});  
 end   
end  

i believe should be ok too. :slight_smile: [import]uid: 21331 topic_id: 27320 reply_id: 111013[/import]

Here is more info about local:
http://blog.anscamobile.com/2012/05/faq-wednesday-4/
[import]uid: 7563 topic_id: 27320 reply_id: 111018[/import]

It is common practice to use local functions, not putting local will put your function into the global space and you will have to manually do clean ups and makes it very hard to follow. An easy way of still using local in your function is doing it the way that TheRealTonyK stated or you can do a forward reference with the function like this:

[lua]–Put the forward reference for the function here
local donePlaying

local isAudioPlaying = false;
local objSnd = audio.loadSound(“myfilepath.mp3”);

local function runSoundFunction(e)
if(e.phase==“began” and isAudioPlaying==false) then
isAudioPlaying = true;
audio.play(objSnd, {onComplete=donePlaying});
end
end

–Now tell the program that it is a function, you are creating the function here but you have already created the reference name. This keeps everything nice and local and allows you to forward reference this function.
donePlaying = function(e)
isAudioPlaying = false;
end

Runtime:addEventListener(“touch”, runSoundFunction)[/lua] [import]uid: 126161 topic_id: 27320 reply_id: 111023[/import]

It sounds like “forward referencing” is telling the code that the function is local, but the function is defined later on. Am I understanding this correctly? (in the layman’s thought-process?)

If so, is there a benefit to this methodology? [import]uid: 154122 topic_id: 27320 reply_id: 111025[/import]

yes that is correct. There are times when you need to be able to call a function before it is created. This is done by forward referencing. By defining the variable as local and then later setting it as a function we are able to keep the same structure of your coding and keep the function local. By calling your function by just function with no forward reference will work BUT if you do not nil out this function then it will stay in global space until the user exits the game. If you do this way too often you could run into memory issues. So by using local functions you save yourself the trouble of having to figure out when you need to nil out your global function and it just makes things easier.

There is a time and a place for global variables and functions just try to keep it minimal so you don’t have to keep up with a lot of globals. [import]uid: 126161 topic_id: 27320 reply_id: 111028[/import]