0

Why no output?

#include <iostream> using namespace std; int factorial(int n) { return n * factorial(n-1); } int main() { cout << factorial(5); }

11th Nov 2018, 7:02 PM
Igor The Golden Fish
Igor The Golden Fish - avatar
7 Answers
+ 6
Without base condition function will keep calling itself recursively infinite times.
12th Nov 2018, 10:21 AM
blACk sh4d0w
blACk sh4d0w - avatar
+ 1
Hello. I think that your recursive function "factorial" is wrong. You need 'if (n==1) {return 1}'. Otherwise the function never stop.
11th Nov 2018, 7:07 PM
Timo Heinzelmann
Timo Heinzelmann - avatar
0
Because you just created endless recursion. You have to write an assertion, when n reaches 0 to stop recursion.
11th Nov 2018, 7:08 PM
Jakub Stasiak
Jakub Stasiak - avatar
0
I don't wont to make the end to this program
11th Nov 2018, 7:09 PM
Igor The Golden Fish
Igor The Golden Fish - avatar
0
So you'll never get result then.
11th Nov 2018, 7:10 PM
Jakub Stasiak
Jakub Stasiak - avatar
0
I wont to create endless recursion but no output
11th Nov 2018, 7:10 PM
Igor The Golden Fish
Igor The Golden Fish - avatar
0
Okey, thanks)
11th Nov 2018, 7:11 PM
Igor The Golden Fish
Igor The Golden Fish - avatar