Properly handling a function which calls itself

36 views
Skip to first unread message

Evagoras Charalambous

unread,
Mar 31, 2015, 3:08:27 PM3/31/15
to lu...@googlegroups.com
Let's say we have a function that returns a string:

public string function GetResponse( struct data required ) localmode="modern" {
     response
= "";
     
// processing logic which sets the var to some string like 'approved'
     
return response;
}

And another function which calls that:

public boolean function RunRefund( struct data required ) localmode="modern" {
    sLocalData
= duplicate(arguments.data);
    response
= GetResponse(sLocalData);
   
if( response === "Approved" ) {
        bSuccessful
= true;
       
// ...
   
}
   
else {
       
bSuccessful = false;
       
if( sLocalData["gateway"] != "paypal" ) {
            sLocalData
["gateway"] = "paypal";
           
RunRefund(sLocalData);
           
// do I need a break here like so:
           
break;
       
}
   
}
   
return bSuccessful;
}

I know I simplified this, but I am getting some mixed results. Am I right in needing to add a break in the second function since it's calling itself, to avoid returning out of it if it needs to call itself?

Igal @ Lucee.org

unread,
Mar 31, 2015, 3:15:53 PM3/31/15
to lu...@googlegroups.com
you don't need a 'break' there because this is not a loop, but rather a recursive call.

you need to make sure that the recursive function has a clean exit strategy -- i.e. that the conditions that make the recursive call aren't met continuously -- or else you will run into a Stack Overflow.

Igal Sapir
Lucee Core Developer
Lucee.org

--
You received this message because you are subscribed to the Google Groups "Lucee" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lucee+un...@googlegroups.com.
To post to this group, send email to lu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lucee/325cecae-a13e-4cc4-b946-4774fb62cf8c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Evagoras Charalambous

unread,
Mar 31, 2015, 3:29:04 PM3/31/15
to lu...@googlegroups.com
Thanks for the quick sanity check! :)
Reply all
Reply to author
Forward
0 new messages