Go to the Next or Previous section, the Detailed Contents, or the Amiga E Encyclopedia.


16.2 Mutual Recursion

In the previous section we saw the function fact_rec which called itself. If you have two functions, fun1 and fun2, and fun1 calls fun2, and fun2 calls fun1, then this pair of functions are mutually recursive. This extends to any amount of functions linked in this way.

This is a rather contrived example of a pair of mutually recursive functions.

PROC f(n)
  IF n=1
    RETURN 1
  ELSEIF n>=2
    RETURN n*g(n-1)
  ELSE
    Raise("F")
  ENDIF
ENDPROC

PROC g(n)
  IF n=1
    RETURN 2*1
  ELSEIF n>=2
    RETURN 2*n*f(n-1)
  ELSE
    Raise("G")
  ENDIF
ENDPROC

Both functions are very similar to the fact_rec function, but g returns double the normal values. The overall effect is that every other value in long version of the multiplication is doubled. So, f(n) computes n*(2*(n-1))*(n-2)*(2*(n-3))*...*2 which probably isn't all that interesting.


Go to the Next or Previous section, the Detailed Contents, or the Amiga E Encyclopedia.