Question:
A questions about recursion and the "return" statement.?
drno
2011-03-19 13:40:08 UTC
The below program is an example for a recursive function. It calculates the factorial of a number. My question is, why do we need the return statement in line - " return (a * factorial (a-1)); ". If i try to compile the code without the return statement, I do not get the correct result.

#include
using namespace std;

long factorial (long a)
{
if (a > 1)
return (a * factorial (a-1));
else
return (1);
}

int main ()
{
long number;
cout << "Please type a number: ";
cin >> number;
cout << number << "! = " << factorial (number);
return 0;
}
Four answers:
The Phlebob
2011-03-19 14:00:43 UTC
You sort of answered your own question: We need the line because the code doesn't work without it.



In detail: The code you deleted is the code that calls factorial() recursively with the next-lower integer. The return (1); line is what unwinds this recursion loop. As soon as it's executed, it returns to the next-higher call of factorial() with a value of 1, which will perform the return you tried removing. That will in turn return to the call of factorial() that called IT, etc.



Hope that helps.



Personally, I've never liked this as an example of recursive programming, as it's easier to implement as a simple loop (there are no variables to retain from level-to-level) or even a simple table lookup. A directory search is a much more realistic example.
Antonio Lawrence
2011-03-19 16:43:45 UTC
First,

you need to understand mathematically what a function is



example:



f(x)=x+5



f(1) = 5

5 is the value of f(1)





The way to assign a value to a function is with the RETURN statement



int f(int x)

{

return ( x + 5)

}



2 RECURSION IS A WAY TO SIMPLIFY A PROBLEM UNTIL YOU REACH THE BASE CASE



IT HAS 2 CASES



case 1

base case: It is when the function stop calling itself and has a value to the function



case 2

recursive case: it calls to itself in order to simplify a problem.



FACTORIAL FUNCTION

recursively

factorial( ) is defined:



base case:

factorial(1) = 1



recursive case:

factorial(a) = factorial(a-1) * a



IN YOUR CODE



long factorial (long a)

{

if (a > 1)

return (a * factorial (a-1)); /***********RECURSIVE CASE *************

else

return (1); /***********BASE CASE *************

}



BYE
2011-03-19 13:45:56 UTC
If a is greater than 1, you want to return a * factorial (a-1) - which is where the recursion is occurring. The function calls itself with an argument of a - 1, until a = 1, then it starts unwinding.
James Bond
2011-03-19 18:35:54 UTC
!5 = 1.2.3.4.5 = !4. 5

!4 = 1.2.3.4 = !3. 4

Thus,

!n = !(n-1) . n

Given

!0 = 1



From this mathematical recursive relation ship, the function can be written as



int fact(int n)

{

if(n==0) return 1;

else

return( fact(n-1)* n);

}



Did you get it?

In my C book, I have included more than 60 examples and their traces.

You may find the same is available at www.ritchcenter.com also.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...