Functions and Factorials why to type return?
Jun 20, 2014 at 11:00am UTC
If I type return faktorijel(rezultat,broj-1);
for 5 result is just 120
and if I type:
faktorijel(rezultat,broj-1);
without return
for number 5 output is:
120
120
60
20
5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
//This is working ok,for example for 5 it is 120
#include <iostream>
using namespace std;
void faktorijel(int rezultat,int broj)
{
if (broj>1)
{
rezultat*=broj;//5 20 60 120
return faktorijel(rezultat,broj-1); //This is my question
}
cout<<rezultat<<endl;
}
int main()
{
int broj;
cout<<"Unesite broj" <<endl;
cin>>broj;
faktorijel(1,broj);
return 0;
}
//This is not working ok
#include <iostream>
using namespace std;
void faktorijel(int rezultat,int broj)
{
if (broj>1)
{
rezultat*=broj;//5 20 60 120
faktorijel(rezultat,broj-1); //without return
}
cout<<rezultat<<endl;
}
int main()
{
int broj;
cout<<"Unesite broj" <<endl;
cin>>broj;
faktorijel(1,broj);
return 0;
}
Jun 20, 2014 at 11:53am UTC
Well. I think if I write another one you'll understand.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
#include <iostream>
//In your case, the return make the code stop and call itself again. (because it's void)
//A better application is that:
unsigned factorial(unsigned number)
{
unsigned result;
if (number <= 1 ) return 1;
result = number * factorial(number - 1); // number * number - 1 * number - 2 .. * [number - (number - 1) = 1]
return result;
}
//So think on this recursion function:
// y = 10
//[sigma] x + y = (0 + 10) + (1 + 10) + (2 + 10) + (3 + 10) ... (10 + 10)
// x = 0
unsigned int sum(unsigned int start_, unsigned int end_)
{
if (start_ != end_ + 1)
return start_ + end_ + sum(start_ + 1, end_); //sum will be evaluated on the recursion call
else return 0;
}
//And try to understand it.
int main(int argc, char * argv[])
{
std::cout << "Factorial of 3. Expected result = 6\n(3x2x1 = 6)\n\n" ;
std::cout << "Calling factorial(3)... " << (factorial(3) == 6? "Passed" : "Failed" );
std::cout << "\n\n" << "Sum of 3. Expected result = 18\n[(0+3) + (1 + 3) + (2 + 3) + (3 + 3)] = 18\n\n" ;
std::cout << "Calling sum(0,3)... " << (sum(0,3) == 18? "Passed" : "Failed" );
std::cout << "\n\nFinal result of recursion functions: " << (sum(0,5) == 45 && factorial(5) == 120? "OK." : "Fail." );
}
Last edited on Jun 20, 2014 at 12:04pm UTC
Jun 20, 2014 at 12:14pm UTC
the return on line 10 prevents the cout on line 12 to be executed. It then jumps out of the function
You can achieve that without return:
1 2 3 4 5 6 7 8 9 10
void faktorijel(int rezultat,int broj)
{
if (broj>1)
{
rezultat*=broj;//5 20 60 120
faktorijel(rezultat,broj-1); // Note: No return
}
else // Note: else instead of return
cout<<rezultat<<endl;
}
Jun 20, 2014 at 12:51pm UTC
@coder777 that's what I said.
iQChange wrote:
In your case, the return make the code stop and call itself again. (because it's void)
Jun 21, 2014 at 9:20am UTC
Thanks to all.
Jun 22, 2014 at 9:43pm UTC
iQChange wrote:In your case, the return make the code stop and call itself again. (because it's void)
No, return ends the execution of a function. It has nothing to do with calling itself
Jun 23, 2014 at 1:38am UTC
RECURSION, GENIUS!
Topic archived. No new replies allowed.