Need Help Understanding Simple Function

Hi, I’m going over some code, and I came across a function I’m having trouble understanding.  Here it is, firstly:

local o = display.newGroup(); o.getStagedataByStageId = function(value) for i=1, #\_G.stages do if \_G.stages[i].stageId == value then return \_G.stages[i]; end end return \_G.stages[1]; end 

I believe this function’s purpose is simply to return a value, but what I don’t understand is that there are two return statements.  Because it’s read last, wouldn’t the one on the bottom override the previous return statement (the one in the if/then statement), making that statement redundant?  In my head, I’m thinking (incorrectly of course) it should say something like:

if\_G.stages[i].stageId == value then return\_G.stages[i]; else return\_G.stages[1], end 

 I’m not an experienced coder, so I’m sure this question is very noobish.  Can anyone help me understand?

 

Thanks in advance,

Steven

The “return” statement in the IF/Then statement would execute if the statement was true and the exit the function. The last “return” statement would only execute if the IF statement was false.

The “return” statement in the IF/Then statement would execute if the statement was true and the exit the function. The last “return” statement would only execute if the IF statement was false.