0

In this code how is stop ?it have been writen in the course of c++

int factorial(int n) { if (n==1) { return 1; } else { return n * factorial(n-1); } }

21st Jun 2018, 6:25 PM
Omar Muhammad Nofal
Omar Muhammad Nofal - avatar
6 Réponses
+ 12
On every value you pass, you see that the function calls itself again but when the value is 1, the function returns a raw number (1) and does not call itself again...
21st Jun 2018, 6:33 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 2
thanks Valen.H. ~
23rd Jun 2018, 9:11 AM
Alex
Alex - avatar
+ 1
every time the function is called the parameter 1 smaller. it stops when the parameter reaches 1
21st Jun 2018, 6:26 PM
Max
Max - avatar
+ 1
because it returns 1 if n = 1 and then the function that called it returns and so on
21st Jun 2018, 6:31 PM
Max
Max - avatar
0
why it's stop in the 1
21st Jun 2018, 6:30 PM
Omar Muhammad Nofal
Omar Muhammad Nofal - avatar
0
thank for explication
21st Jun 2018, 6:48 PM
Omar Muhammad Nofal
Omar Muhammad Nofal - avatar