I was wondering how (or maybe even if it’s possible) to implement recursion without a separate function that calls itself.
So far all algorithms implementing recursion I’ve seen use a separate function. I thought a lot and came up with the idea that a goto statement with some variable mutation can do the job but I’m really unsure about that.
I made a mini research and found info about this Structured programming theorem which proves that every algorithm can be implemented with only three data structures, so such a recursion implementation must be posible but I still cannot assemble everything into consistent knowledge and understanding for the whole would-be approach.
I was wondering how (or maybe even if it’s possible) to implement recursion without
Share
What you are looking for is basically expressing a recursive function into an iterative form.
This can easily be done by using a Stack. Here’s a very simple example in C#:
Here’s the exact same function in iterative form: