Technote (FAQ)
Question
Are recursive functions supported by OPL script?
Answer
Yes, recursive functions are supported by OPL script. One such function to find the sum of first N positive integers could be written in an execute block as such:
execute{
function recursiveSum(x){
writeln("Finding sum upto " + x + " ...");
var result;
if(x > 1){
result = x + recursiveSum(x-1);
}
else{
result = 1;
}
writeln("Sum upto " + x + "=" + result);
return(result);
}
var testNumber = 5;
var testResult = recursiveSum(testNumber);
writeln("Sum of the first " + testNumber + " positive integers=" + testResult);
}
| Segment | Product | Component | Platform | Version | Edition |
|---|---|---|---|---|---|
| Business Integration | IBM ILOG CPLEX Optimization Studio | Area:ILOG Script for OPL/OPL Script | AIX, Linux, Windows | 12.4, 12.3, 12.2.0.1, 12.2 | All Editions |
Rate this page:
Copyright and trademark information
IBM, the IBM logo and ibm.com are trademarks of International Business Machines Corp., registered in many jurisdictions worldwide. Other product and service names might be trademarks of IBM or other companies. A current list of IBM trademarks is available on the Web at "Copyright and trademark information" at www.ibm.com/legal/copytrade.shtml.