hey guys.. i know how to factorial, my problem is how can i do when i used function call...
int main()
{
int n;
int factorial,i;
cout << "Enter a positive integer: ";
cin >> n;
for (i = 0; i <= n; i++)
if (i == 0)
factorial = 1;
else
factorial = factorial * i;
cout << "The factorial of " << n << " is " << factorial << endl;
now, i need to used function call.. what i will do??
You should avoid using recursion unless it massively simplifies your problem. It's just a neat way of slowing things down. Don't use that example Diemon, especially if this is homework, because your teacher will definitely know it's not your own code =P
kbw is on the money, this has been answered a million times before, go and find it.
Recursion doesn't slow down anything if you know what you are doing :P
Your compiler did tail call optimization. Unfortunately, there is no standardized way to force it. I like recursion. It somehow emphasizes the invariants in the code. But it can cause nasty business in embedded systems with small stack (low-end low-consumption hardware), and is generally not as effective as iteration in non-functional languages.
Unfortunately, there is no standardized way to force it
True, but this is the limitation of the language/compiler not the recursion per se. Ofc, I don't recommend using recursion in languages that don't have proper way to enforce TCO optimisations. Nevertheless coding in a recursive style is a good brain-excercise. :)