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.
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?