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
Commerce 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

Document information


More support for:

IBM ILOG CPLEX Optimization Studio
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

Machine Translation

Content navigation