Had a similar question recently concerning gdscript because some things just need a lot of recursion, like building spacial data structures or selecting elements for rendering or physics tests. The clear answer was "Yes, gdscript supports recursion" @cybereality . Haven't tried gdscript yet because am still in C++.
Recursion isn't difficult. Enter the function, first(!) test condition if the end is reached, if yes, return fixed value. If not, return function call with new parameters. Recursion takes place on the stack, so do a short estimation if that's not limiting the thing, or else bust.
Sure you can do any recursion also iteratively, but it is usually a lot more to write (and thus more difficult to maintain). Compilers these days are good ... scrub ... interpreted language. Nevertheless, to recurse is is advisable usually when a problem can be divided such that each sub-problem has the same solution path. Path finding, ray/path tracing, graphs, directory trees, these kind of things.
Classical example you could plug in to check (also the size of the stack ;-)): Fibonaccy
int fibonacci(int n) {
if(n == 0)
return 0;
else if(n == 1)
return 1;
else
return (fibonacci(n-1) + fibonacci(n-2));
}
Edit: here you go, took just a few seconds, my first own gdscript not typed from some turoial 🍾
extends Spatial
func fib(n):
if(n == 0):
return 0;
elif(n == 1):
return 1;
else:
return(fib(n-1) + fib(n-2))
func _ready():
print("fib(10): ", fib(10))