Using recursion when plain old iteration would have been just as good, NO, just NO.
If it really is an iteration then any decent modern compiler should optimise the tail-calls.
Relevant paper from 47 years ago in 1977:
https://en.wikisource.org/wiki/Lambda:_The_Ultimate_GOTO#include <stdio.h>
typedef int status;
status need(void *params);
status write(void *params);
status begin(void *params);
status need(void *params) {
printf("in need\n");
return write(params);
}
status write(void *params) {
printf("in write\n");
return begin(params);
}
status begin(void *params) {
printf("in begin\n");
return need(params);
}
int main(){
int param;
need(¶m);
return 0;
}
Compiled with ...
debian@lpi4a:~$ gcc -Os fp.c -o fp
debian@lpi4a:~$ ./fp | head
in need
in write
in begin
in need
in write
in begin
in need
in write
in begin
in need
debian@lpi4a:~$
The machine code:
00000000000005d0 <main>:
5d0: 1101 add sp,sp,-32
5d2: 0068 add a0,sp,12
5d4: ec06 sd ra,24(sp)
5d6: 102000ef jal 6d8 <need>
5da: 60e2 ld ra,24(sp)
5dc: 4501 li a0,0
5de: 6105 add sp,sp,32
5e0: 8082 ret
...
000000000000069c <begin>:
69c: 1141 add sp,sp,-16
69e: e022 sd s0,0(sp)
6a0: 842a mv s0,a0
6a2: 00000517 auipc a0,0x0
6a6: 05e50513 add a0,a0,94 # 700 <_IO_stdin_used+0x8>
6aa: e406 sd ra,8(sp)
6ac: f15ff0ef jal 5c0 <puts@plt>
6b0: 8522 mv a0,s0
6b2: 6402 ld s0,0(sp)
6b4: 60a2 ld ra,8(sp)
6b6: 0141 add sp,sp,16
6b8: a005 j 6d8 <need>
00000000000006ba <write>:
6ba: 1141 add sp,sp,-16
6bc: e022 sd s0,0(sp)
6be: 842a mv s0,a0
6c0: 00000517 auipc a0,0x0
6c4: 05050513 add a0,a0,80 # 710 <_IO_stdin_used+0x18>
6c8: e406 sd ra,8(sp)
6ca: ef7ff0ef jal 5c0 <puts@plt>
6ce: 8522 mv a0,s0
6d0: 6402 ld s0,0(sp)
6d2: 60a2 ld ra,8(sp)
6d4: 0141 add sp,sp,16
6d6: b7d9 j 69c <begin>
00000000000006d8 <need>:
6d8: 1141 add sp,sp,-16
6da: e022 sd s0,0(sp)
6dc: 842a mv s0,a0
6de: 00000517 auipc a0,0x0
6e2: 04250513 add a0,a0,66 # 720 <_IO_stdin_used+0x28>
6e6: e406 sd ra,8(sp)
6e8: ed9ff0ef jal 5c0 <puts@plt>
6ec: 8522 mv a0,s0
6ee: 6402 ld s0,0(sp)
6f0: 60a2 ld ra,8(sp)
6f2: 0141 add sp,sp,16
6f4: b7d9 j 6ba <write>
I can let that run all day without stack overflow.