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).
© 2001-2020 Fair Isaac Corporation. All rights reserved. This documentation is the property of Fair Isaac Corporation (“FICO”). Receipt or possession of this documentation does not convey rights to disclose, reproduce, make derivative works, use, or allow others to use it except solely for internal evaluation purposes to determine whether to purchase a license to the software described in this documentation, or as otherwise set forth in a written software license agreement between you and FICO (or a FICO affiliate). Use of this documentation and the software described in it must conform strictly to the foregoing permitted uses, and no other use is permitted.