Why this output is coming? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 2

Why this output is coming?

Can somebody explain me the output of the below C code. #include <stdio.h> int fact(int n) { if(n==1) return 1; else { printf("%d>>", n*fact(n-1)); return (n*fact(n-1)); } } int main() { fact(5); return 0; } Also if you comment the "return" statement in the fact function the output is something which I'm not able to understand. Can someone explain me this please. Thank you. :) https://code.sololearn.com/cAsgB6NOaSq7/?ref=app https://code.sololearn.com/cAsgB6NOaSq7/?ref=app

28th Jul 2018, 4:00 PM
Aashish Kumar Shaw
Aashish Kumar Shaw - avatar
12 Respostas
+ 1
Aashish Kumar Shaw Ok, I see. For x86 at least, eax is used as a return register. It means anything that was there will be considered to be the return value. Local variables can also be stored in this register during function execution. Therefore it is possible that function returns a value of any variable. Sometimes eax may store the correct value and it will be returned, but in general lack of the return statement causes undefined behavior. In your case the output is 2, 9, 12, 20. Respectively, it had to print 1*2, 2*3, 3*4, 4*5. Perhaps 3 was returned by function instead of 2 and that is why 3*3 is printed, not 2*3.
29th Jul 2018, 4:50 PM
Steppenwolf
Steppenwolf - avatar
+ 1
Parastoo what you wrote is a simple factorial function which calculates factorial of a number.My question was actually why the fact() was returning such values.
30th Nov 2018, 5:28 PM
Aashish Kumar Shaw
Aashish Kumar Shaw - avatar
+ 1
I finally got the answer. If you draw the recursive tree of the fact() function you will get those values. The values which are coming in output are nothing but value of nodes of the fact() function when traversed.
30th Nov 2018, 5:30 PM
Aashish Kumar Shaw
Aashish Kumar Shaw - avatar
0
Since it is a recursive function, it is being executed several times. Each time it is executed, it prints out its temporary calculations. You should comment out the whole 8 line. Use printf("%d", fact(5)); in main instead. Commenting out 5 line causes an error due to the lack of the semicolon in the if statement.
29th Jul 2018, 12:54 PM
Steppenwolf
Steppenwolf - avatar
0
Steppenwolf I know that to get the factorial I should use printf in main(). I just want to know why it is calculating that way. What is temporary calculation and why its done in this way?
29th Jul 2018, 1:00 PM
Aashish Kumar Shaw
Aashish Kumar Shaw - avatar
0
Aashish Kumar Shaw Factorials are being split upĀ forming such a recursion tree. Based on such a tree, you can trace, when the function with a certain argument is being executed. Look up in google images for the example of a recursion tree. Notice that the factorials of 2!, 3!, 4!, 5! appear in the output. These are values returned by inner functions on each stage. With the function termination tree unites and prints out its temporary result that is used by outer function for further calculations. It is a bit difficult for me to explain it in words, but I hope I gave you some conception of how do I personally understand that.
29th Jul 2018, 3:14 PM
Steppenwolf
Steppenwolf - avatar
0
Steppenwolf Please comment the 9th line and check the output.
29th Jul 2018, 3:24 PM
Aashish Kumar Shaw
Aashish Kumar Shaw - avatar
0
Aashish Kumar Shaw If I comment it, function won't be able to specify a return value to be passed back to the code as there will be no return for other arguments that 1. That might cause problems I guess. Btw, why do you want to print n*fact(n-1)? In fact, you call this function recursively twice in each function. Once for printing, once for calculating factorial. Maybe printf("%d", n) instead of printf("%d", n*fact(n-1)) will solve your understanding problem? ;)
29th Jul 2018, 3:53 PM
Steppenwolf
Steppenwolf - avatar
0
Steppenwolf I don't want to print n*fact(n-1) ..I was just wondering what output it will give if I execute it and then I found some weird results. But if u comment the 9th line then the function is returning something ..so my question is what value is the function returning that when we comment the 9th line we get output as 2>>9>>12>>20
29th Jul 2018, 4:11 PM
Aashish Kumar Shaw
Aashish Kumar Shaw - avatar
0
I'm not sure, but I think that makes sense.
29th Jul 2018, 4:52 PM
Steppenwolf
Steppenwolf - avatar
0
Yup, it makes sense. I appreciate your efforts towards my question. But If I can get to know the exact reason why It's returning 3*3= 9 I'll sure post it here. Thank you. Steppenwolf
29th Jul 2018, 4:56 PM
Aashish Kumar Shaw
Aashish Kumar Shaw - avatar
0
i don't know a lot from c but i corrected your code according to my knowledge of c++ . this code works properly. try it! #include <stdio.h> int fact(int n) { if(n==1||n==0) return 1; else return n*fact(n-1) ; } int main() { printf("%d>>",fact(5)); return 0; } this is another form of the same code: #include <stdio.h> int fact(int n); int main() { printf("%d>>",fact(5)); return 0; } int fact(int n) { if(n==1||n==0) return 1; else return n*fact(n-1) ; }
30th Nov 2018, 5:23 PM
Parastoo Bahmani