Skip to main content

Writing recursive functions using OPL script


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);
}


Cross reference information
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:

(0 users)Average rating

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.

Rate this page:


(0 users)Average rating

Add comments

Document information

IBM ILOG OPL-CPLEX Development Bundles

Area:ILOG Script for OPL/OPL Script


Software version:
6.0, 6.0.1, 6.1, 6.1.1, 6.2, 6.3, 12.2, 12.3, 12.4


Operating system(s):
AIX, Linux, Windows


Software edition:
All Editions


Reference #:
1615791


Modified date:
2012-11-14

Translate my page

Content navigation