Recursion
The following example (model lcdiv2.mos) returns the largest common divisor of two numbers, just like the example `Lcdiv1' in the previous chapter. This time we implement this task using recursive function calls, that is, from within function lcdiv we call again function lcdiv.
model Lcdiv2
 function lcdiv(A,B:integer):integer
  if(A=B) then
   returned:=A
  elif(A>B) then
   returned:=lcdiv(B,A-B)
  else
   returned:=lcdiv(A,B-A)
  end-if
 end-function
 declarations
  A,B: integer
 end-declarations
 write("Enter two integer numbers:\n  A: ")
 readln(A)
 write("  B: ")
 readln(B)
 writeln("Largest common divisor: ", lcdiv(A,B))
end-model This example uses a simple recursion (a subroutine calling itself). In Mosel, it is also possible to use cross-recursion, that is, subroutine A calls subroutine B which again calls A. The only pre-requisite is that any subroutine that is called prior to its definition must be declared before it is called by using the forward statement (see below).
 
