Which one will return value 24 ?I can't understand the meaning of a*f(a-1) and the whole logic. Can everyone tell me??Code:int f(int a) { if (a < 1) return 1; else return a * f(a - 1); } (A) f(2) (B) f(3) (C) f(4) (D) f(6)
Thanks!!!
Which one will return value 24 ?I can't understand the meaning of a*f(a-1) and the whole logic. Can everyone tell me??Code:int f(int a) { if (a < 1) return 1; else return a * f(a - 1); } (A) f(2) (B) f(3) (C) f(4) (D) f(6)
Thanks!!!
Last edited by Salem; 12-11-2014 at 12:04 AM. Reason: fixed train-wreck formatting
The formatting of your post is atrocious. Just post your question in plain text, with any actual code in \[code\] tags.
Read up on recursion, here's the site's lesson on it: Recursion in C and C++ - Cprogramming.com
Sorry, guys.
I write it in correct way.
I can't understand the meaning of a*f(a-1) and the whole logic.Code:int f(int a) { if (a<1) return 1; else return a*f(a-1); }
Which one will return value 24?
(A) f(2)
(B) f(3)
(C) f(4)
(D) f(6)
Last edited by tzungshianlin; 12-11-2014 at 09:20 PM.
Formatting is much better, but still needs a little work:
Did you read the link nonpuz provided you in post #2? Click the red text.Code:int f(int a) { if (a<1) return 1; else return a*f(a-1); }
Next, get a piece of paper and pencil, and trace the code by hand for each choice A-D. Start with A and tell us what you think f(2) returns, and how you figured that out.