Alot of c compilers can optimize a function that is written this way
Code:
void print_hello(int n)
{
if (n != 0) {
printf("Hello\n");
print_hello(n - 1);
}
}
The output from gcc with optimization is
Code:
print_hello:
pushl %ebp
movl %esp, %ebp
pushl %ebx
pushl %eax
movl 8(%ebp), %ebx
.L19:
testl %ebx, %ebx
je .L18
subl $12, %esp
pushl $.LC0
call printf
decl %ebx
addl $16, %esp
jmp .L19
.p2align 2
.L18:
movl -4(%ebp), %ebx
leave
ret
So a iterative solution written recursively doesn't always eat up stack space.