Factorial in CPL

Example for versions CPL

(Example from language description) This code shows an iterative way to calculate factorial.

result of clause allows to use a block as an expression. The body of the block must contain an assignment to variable result. The block may contain local variables, but nothing which could result in a side effect (for example, changing the values of external variables is prohibited). This clause is typically used with function definitions.

Loop body uses simultaneous assignment of new values to the variables. Both of them use small names, so the language allows to skip multiplication sign between them — xf is treated same as x*f.

function Fact2[x] = result of
    § real f = 1
      until x = 0 do
          f, x := xf, x  1
      result : = f §