AnsweredAssumed Answered

Loss of variable value in recursive custom functions?

Question asked by FrankvanderMost on Mar 20, 2015
Latest reply on Oct 5, 2015 by user19752

The following recursion function called test_recursion with one operator called number will work, i.e. it sums up the range 0+1+2+...number

 

Let ( $step = number ;

 

    Case (

        $step = 0 ; 0 ;

 

        $step + test_recursion (  $step - 1 )

    )

)

 

 

However if instead of '$step + test_recursion (  $step - 1 )' I use 'test_recursion (  $step - 1 ) + $step', it will not work (the result of a function call is always 0).

In my understanding of recursion both should work since the recursions are stacked.

 

After some testing, I found out that one recursion forgets its variable value of $step after returning from the recursive call. Try using  "'" & $step & "'" & test_recursion (  $step - 1 ) & "'" & $step & "'"  instead of  $step + test_recursion (  $step - 1 ) 

 

Does anyone know if this is normal behavior and if so why this is?

 

I am guessing that it has to do with the fact that FMP's function engine is optimised for tail-recursion, but if regular recursion is possible (even though not optimized) it should work too.

Outcomes