What's the point of recursion when you can just use a while loop?
While loops require state, which is inherently error-prone.
>>60029775
>require state
What's state?
>error-prone
What's an example that will fail in a while loop but doesn't in a recursion
>>60029709
What's the point of a loop, when you can just write a recursive function?
>>60029775
Honestly, this is the most retarded sentence in the context of programming I have ever read in life.
>>60029709
If you structure your recursive function properly, the compiler actually should translate it into a while loop (tail recursion).
Beyond that, it's a bit of an encapsulation thing, you know exactly what's going into and out of the function so it's easier to debug in a lot of situations.
>>60029775
>>60029837
But the poster was right, so if they were right and you disagree with them, you're an idiot who doesn't know what recursion or loops even really are.
>>60029709
Use whichever one is most readable for the situation.
Often with a tree structure recursion comes naturally.
>>60029709
It makes you feel smart
>>60029866
>Often with a tree structure recursion comes naturally.
This.
>>60029808
Whats the use of a recursive function if you can just copy and paste it
>>60030256
>Whats the use of a recursive function if you can just copy and paste it
What? Are you actually mentally retarded, or just trying to be?
If you copy the function, you can't call into it a variable number of times.
>>60030281
>not getting the joke
>calling others retarded
>>60030482
> trying to make a joke
> failing hard
>>60029709
>What's the point of recursion when you can just use a while loop?
Tree traversal made easier
https://www.youtube.com/watch?v=Qk0zUZW-U_M
the pajeets has evolved
With TCO those two codes are equivalent.static int rec_fact_loop (int accu, int n) {
if (n <= 1) {
return accu;
} else {
return rec_fact_loop (accu * n, n - 1);
}
}
int rec_fact (int n) {
return rec_fact_loop (1, n);
}
int while_fact (int n) {
int res;
res = 1;
while (n > 1) {
res *= n;
n--;
}
return res;
}
>>60029775
>>60029709
Because loops are inelegant as sh*t!
>>60029775
congratulations you dumb fuck