Functions can call themselves recursively. Each time execution
passes into the function the local variables are (re)created. There
is a special keyword: $self
, which must be used to force a
function to refer to itself.
fac = function ( a ) { if(a <= 1) { return 1; else return a*$self (a-1); } };
In the previous example a factorial computation is performed using recursion12. In the second return statement, the function calls itself until a≤1.