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
Further explanation of this example: 'Mosel User Guide', Chapter 9 Functions and procedures

subroutines.zip[download all files]

Source Files





subrout.mos

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

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

model "Simple subroutines"

 declarations
  a:integer
 end-declarations

 function three:integer
  returned := 3
 end-function

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

 procedure print_start
  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


 print_start 
 a:=three
 writeln("a = ", a)
 a:=times_two(a)   
 writeln("a = ", a)
 hide_a_1
 writeln("a = ", a)
 hide_a_2(-10)
 writeln("a = ", a)
 hide_a_3(a)
 writeln("a = ", a)

end-model 

Back to examples browserPrevious exampleNext example