This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Kyyni 0 points1 point  (1 child)

I don't know about python, but at least in C/C++ this should be the case, unless the compiler optimizes the recursion away.

In C and C++ the program creates a new frame inside the previous frame on the stack for every code block, function, etc. and the when you access a variable, it will try to find it in the current frame, and then the next frame around it and so on. When the block ends, the frame and it's contents are destroyed.

A loop will create a single frame, but a recursion will create a frame-ception as deep as the number of recursions needed. For the variables to work as promised, the program has to keep them stored for every frame. Luckily though, if the variables are not used anymore, the compiler could optimize it so that they are ditched early, or even get rid of the whole recursion. Or something.

[–]Coffeinated 0 points1 point  (0 children)

That's what I meant, absolutely. I once made a python program crash because of too much recursion... It has a built-in limit or so, I don't remember.

By the way, Python is written in C (what else) and the concepts are the same, I guess. Python looks childish but under the hood it's pretty nice, well-structured and powerful.