Properly handling a function which calls itself

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?

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 http://lucee.org/On 3/31/2015 12:08 PM, Evagoras Charalambous wrote:

Let’s say we have a function that returns a string:

|
publicstringfunctionGetResponse(structdata required )localmode=“modern”{
response ="";
// processing logic which sets the var to some string like ‘approved’
returnresponse;
}
|

And another function which calls that:

|
publicbooleanfunctionRunRefund(structdata 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;
}
}
returnbSuccessful;
}
|

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?

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+unsubscribe@googlegroups.com
mailto:lucee+unsubscribe@googlegroups.com.
To post to this group, send email to lucee@googlegroups.com
mailto:lucee@googlegroups.com.
To view this discussion on the web visit
https://groups.google.com/d/msgid/lucee/325cecae-a13e-4cc4-b946-4774fb62cf8c%40googlegroups.com
https://groups.google.com/d/msgid/lucee/325cecae-a13e-4cc4-b946-4774fb62cf8c%40googlegroups.com?utm_medium=email&utm_source=footer.
For more options, visit https://groups.google.com/d/optout.

Thanks for the quick sanity check! :slight_smile: