# How to stop a recursion in GHpython

Hi All
I am trying a simple example on GHpython (please see attached) and got stuck at the end of the recursive function by its meaning.
I know it is a kind of a way to stop the recursion but cannot understand the meaning behind the code.

I would be so grateful of someone can explain these lines to me in simple English so I can understand it , thank you so much in advance!!

yutaka

reccusion stop.gh (18.4 KB)

See attached, get the gist of it (itâ€™s C# - BTW: I hate P) and keep going.

Recursion_ForAmateurs_V1.gh (11.4 KB)

Note: You can add numerous things/parameters (like a wind direction vector, random min/max generations â€¦ blah, blah)

Thanks Peter! waht a fantastic script! Will certainly learn from it.

just to clear my foggy brain,

Can you or someone tell me what the code I have marked down mean ? Thanks!!

Well â€¦ thereâ€™s no adios amigos question around: something (an if clause) to tell the Method that game is over. (see public vars Loops. Loop in the C# above).

Note the usage of the found bool: since lines â€śsrinkâ€ť" per Loop â€¦ is possible to end with tiny segments that are not a very good thing. So a 2nd if is required.

Lesson of the way: in Recursion PRIOR anything else mastermind some safe exit.

Some tips:

1. In real life a Recursion in most of cases terminates PRIOR the max Loops are reached. Obviously Iâ€™m talking with some serious piece of code in mind - not just a few lines,
2. Learn what Stack and Heap are:
Achieving stack and heap safety in recursive functions | by Bertjan Broeksema | bigdatarepublic | Medium
3. Use public vars (static or not) in order to avoid the obvious.
4. Use public collection(s) (say DataTrees or - better - some other collection) for your results.
5. Be modest with your Loops var (use a secondary [ better safe than sorry] exit clause when testing the thing).
6. Always try to achieve tail recursion.
7. If computer hangs > go ride a proper Ducati (and forget all that).

When you first call the `recursion()` function you need to pass it an initial line and an integer number for `gen`, say 10. Now, the function will call itself recursively as long as `gen` is greater than 0 and before that decrement `gen` by one.
I think line 25 should read `return recursion(newline, gen)` though.

1 Like

Find 5 minutes more for that entry level thingy: Added Wind and Gravity. Spot the obvious: Method SHOULD terminate (in real-life) PRIOR max Loops â€¦ if you want some sort of rational results (so mastermind proper exit clauses).

Recursion_ForAmateurs_V1A.gh (122.2 KB)

Thanks DA

I understand if gen> 0 will run the recursion
How does the decrement by one ends the recursion?

Thank you so much!

Yes, each time the function is called it, decrements `gen` by 1, then if `gen` is still greater than 0, it calls itself with the decremented `gen` value. Otherwise, if `gen` is equal to 0, it simply stops and None gets returned, since youâ€™re not returning anything else at the end of the function.

Many thanks does it mean that the recursion starts to count down ?
Letâ€™s say I initiate gen of 5 in total :
Then it starts to work like this ?

Iteration

5 -1 = 4

4 -1 = 3

â€¦

1 -1 =0 ( stop)

Yes, exactly.

Exactly right. When it reaches 0 the final recursion is not called so the function exits.
More on recursion with Python here:

1 Like

Thanks for the recommendation Graham will certainly take a look!

Just wonder what happens if I didnâ€™t use the
gen=gen -1

In that case for example, would a total of 5 gens loop on forever like this:

5 gens. (Loop 1)

10 gens ( Loop 2)
â€¦
100 gens (Loop 20)

Etc?

From a real-world perspective, the original code is so simple that it shouldnâ€™t use a recursion.

If you donâ€™t decrement `gen`, than your function will call itself infinitely, but the program will probably stop at the default recursion limit that is set in Python by your OS.

1 Like

IronPython doesnâ€™t, by default, have a limit on recursion. So itâ€™s likely Rhino is going to crash when recursion goes infinitely.

1 Like

I see well understood now, many thanks everyone for helping out !
Will keep going !