FICO
FICO Xpress Optimization Examples Repository
FICO Optimization Community FICO Xpress Optimization Home
Back to examples browserPrevious exampleNext example

Subroutines

Description
  • lcdiv2.mos: Recursive function calls
  • primefct.mos: function returning a set
  • qsort1.mos: 'forward' definition of subroutines
  • qsort2.mos: Overloading of subroutines
  • shsortfct.mos: Function returning an array
  • subrout.mos: Local and global declarations, fixed and variable number of arguments
  • reftosubr.mos: Working with subroutine references, using mmreflect functionality for retrieving and calling subroutines
Further explanation of this example: 'Mosel User Guide', Chapter 9 Functions and procedures

subroutines.zip[download all files]

Source Files
By clicking on a file name, a preview is opened at the bottom of this page.
lcdiv2.mos[download]
qsort1.mos[download]
qsort2.mos[download]
subrout.mos[download]
reftosubr.mos[download]





subrout.mos

(!*******************************************************
   Mosel User Guide Examples
   =========================

   file subrout.mos
   ````````````````
   Simple subroutines.
   
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, 2001, rev. Jun. 2022
  *******************************************************!)

model "Simple subroutines"

 declarations
   a:integer
 end-declarations

 function three:integer
   returned := 3
 end-function

 function timestwo(b:integer):integer
   returned := 2*b
 end-function

 procedure printstart
   writeln("The program starts here.")
 end-procedure

 procedure hide_a_1
   declarations
     a: integer
   end-declarations
 
   a:=7
   writeln("Procedure hide_a_1: a = ", a) 
 end-procedure

 procedure hide_a_2(a:integer)
   a:=2
   writeln("Procedure hide_a_2: a = ", a) 
 end-procedure

(! This version generates an error because 'a' is declared twice
 procedure hide_a_3(a:integer)
   declarations
     a: integer
   end-declarations

   a := 15
   writeln("Procedure hide_a_3: a = ", a) 
 end-procedure
!)

! Corrected version of hide_a_3
 procedure hide_a_3(aa:integer)
   declarations
     a: integer
   end-declarations

   a := 15
   writeln("Procedure hide_a_3: a = ", a, ", aa = ", aa) 
 end-procedure

 printstart 
 a:=three
 writeln("a = ", a)
 a:=timestwo(a)   
 writeln("a = ", a)
 hide_a_1
 writeln("a = ", a)
 hide_a_2(-10)
 writeln("a = ", a)
 hide_a_3(a)
 writeln("a = ", a)


! Subroutines with variable number of arguments
 function sumall(Values:...): integer
   returned:= sum(i in Values) i.integer
 end-function

 procedure showint(Optargs:...)
   forall(i as counter, v in Optargs)
     if v is integer then writeln("arg ", i, ": ", v.integer); end-if
 end-procedure

 writeln("sum = ", sumall(a, 1, 2, 3, 4, 5))

 showint(1.5,0,"abc",true,5)

end-model 

Back to examples browserPrevious exampleNext example