Contents Previous Next Subchapters

Forward And Joint Reference Of Functions
See Also recursion

A function can reference another function that is defined after it provided that the definitions are enclosed within a block . The following text defines two functions that reference each other and are enclosed within one block:
     function fac1(n) begin
          if n==1 then return 1
          return n * fac2(n - 1)
     function fac2(n) begin
          if n==1 then return 1
          return n * fac1(n - 1)
If you then enter the text above followed by
     print fac1(3)
O-Matrix will respond
The reference fac1(3) caused the following evaluations:
     function fac1(3) begin
          if 3==1 then return 1
          return 3 * fac2(2)     # fac2(2) returns 2 * 1
     function fac2(2) begin
          if 2==1 then return 1
          return 2 * fac1(1)      # fac1(1) returns 1
     function fac1(1) begin
          if 1==1 then return 1   # returns 1
          return 1 * fac2(0)