A recursive function calls itself until a complex task is solved, whereby the complexity of the task is reduced by the recursive function.
This is how a recursive function is generally explained.
In Filemaker practice, a recursive custom function is especially useful when a series of tasks has to be solved without using a script loop.
Programming a recursion seems difficult at first, because you have to imagine what will happen in the next run of the function, or as the mathematicians say:
To understand recursion, you have to understand recursion.
The most descriptive way to explain a CF in Filemaker is to use a list editing function.
I have created a simple template for a recursive function myself, which I then adapt as needed:
cf_recursive ( theList ; counter ; result )
Let (
[
max = ValueCount ( theList ) ;
counter = If (counter = 0 ODER counter = "" ; 1 ; counter ) ] ;
Case (
counter < max ; cf_recursive (theList ; counter + 1 ; result + GetValue (theList ; counter) ) ; counter = max ; result + GetValue (theList; counter)
)
)
In the Let part of the function the number of calls (called iteration) is determined and a counter is initialized.
These values could be stored in local $ variables too, I prefer to store these values in parameters for debugging reasons.
As an example we call the custom function with a simple number list cf_recursive ( 0¶1¶1¶2¶3¶5¶8¶13 ; 1 ; ““ ).
At every run the max value is set to 8 and the counter is increased by 1.
If the counter < max (i.e. the max value or last list element is not yet reached), the function is called again and the parameters are changed:
unchanged number list
counter increased by one
new result = current result + list value with the index counter
This will execute the same function again, but now the parameters have changed.
The last iteration calculates only the result value and then leaves the function and thus returns the result.
In addition to such simple functions, very complex tasks can be realized with such a custom function, as we will see in the article „Multi portal filter“.
In principle the same result could be achieved with a script loop, but the advantage of a custom function is that it can be called from anywhere and is very performant.
We save a lot of code lines with this function !
But there are also disadvantages.
The limit for the number of iterations is 50000 according to Filemaker.
But from my own experience I would set the limit much lower, at about 1000 iterations.
After that the function becomes very slow or even unusable.
Since Filemaker 18 there is a native filemaker function for the kind of recursive function shown above:
SetRecursion ( expression ; maxIterations )
Our example function would look like this with this function as custom function:
cf_recursive ( theList )
Let (
[
max = ValueCount ( theList )
] ; SetRecursion (
While (
[ i = 0 ; out = "" ] ;
i < max ;
[
i = i + 1 ;
out = out + GetValue (theList; i)
] ;
out
) ; max )
)
The structure is more elegant and with fewer transfer parameters possible.
Another advantage according to Filemaker is that there is no limit for iterations.
I myself experience it in the rarest cases that a recursive function works flawlessly at first go.
But if you approach a complex task in small steps, a recursive function is a very powerful tool.
And yes, the list used in our example is a fibonacci sequence 🙂
You have any questions? Feel free to send me an email, I’ll try to answer as soon as possible.