Does return multiply or adds a function that has an existing value?

I'm kind of confused on this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

using namespace std;

int factorialFinder(int x){
	if (x==1){  
		return 1;
	}else{
		return x*factorialFinder(x-1);
	}
}

int main()
{
	cout << factorialFinder (5) << endl;  
}


I was under the impression the return gives a value to the function, but if it gives a value then this should print out "1". Instead this prints out "120". I thought this was kind of weird so I put 'return 2;' inside the if (x==1) brackets. It printed out '240'!

In the else bracket I put 'return x+factorialFinder (x-1);' with with the same 'if (x==1){return 1}' and I got 15. When I put 'return 2' I get 16. When I put 'return -2" I get 12. And when I put 'return x-factorialFinder(x-1);' and put 'return 2' I get 4 (still adds on 2).

My new assumption is that an 'int' function already has a value of 1 and return multiplies it by default with a number you give it like:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

using namespace std;

int example(){
    return 22;
}


int main()
{
	cout << example();
}



and you get 22. But as with most of my 'assumptions' it's usually wrong. Can someone help clarify this for me?
Last edited on
Factorial of n is product of all integer values from 1..n.

So 3! = 1 * 2 * 3.

It is usually convenient to compute it from n to 1:

3! = 3 * 2 * 1.

Likewise, 4! = 4 * 3 * 2 * 1.


A function returns a specific value. Hence, example() returns 22.

But what if you have 2*example()? You get 2*22 = 44.
Likewise, 3*example() gets you 3*22 = 66.

The factorialFinder() function is doing the same thing. If its argument is 1, it just returns 1.

Otherwise, it calls itself.

factorialFinder(2) == 2*factorialFinder(2-1) == 2*factorialFinder(1) == 2*1 == 2.

Likewise,
factorialFinder(3) ==
3*factorialFinder(3-1) ==
3*factorialFinder(2) ==
3*2*factorialFinder(2-1) ==
3*2*1 == 6.

Hope this helps.
I'm still confused, but I'm not confused about the else statement part. I'm confused about the 'return 1' part in inside the if statement.

Here's my understanding.

1. The computer goes to "factorialFinder (5)".

2. The value of 5 goes to factorialFinder(int x). Since 5 is not equal to 1 it goes to the else statement.

3. It will do what you say until it reaches 1 (5*(5-1), 20*(4-1), 60*(3-1), 120*(2-1))

**4. (the part I'm having the problem with) When x finally does equal to 1 it will go to the if statement and return 1, as in overwrite factorialFinder () with a value of 1

5. It then prints out "1"

I know this is wrong because it prints out 120.. My assumption is that return multiplies the value with the number you give it instead of giving the function the value of 1. Is this true?
I know this is wrong because it prints out 120.. My assumption is that return multiplies the value with the number you give it instead of giving the function the value of 1. Is this true?


No. The function multiplies the value returned on line 9, then it returns the resulting value.
What you have here is a recursive function; which returns a factorial number. So when "5" is put in, it would return 120 because the function does the following:

5*4*3*2*1

Because when x != 1, it would return the function again using a x-1 as the perimeter. Once x does equal 1, it would return the 1 which would be at the end of all values being multiplied.

factorialFinder(5) :: return 5 * (factorialFinder(5-1) :: return 4 * (factorialFinder(4-1) :: return 3 * (factorialFinder(3-1) :: return 2 * (factorialFinder(2-1) :: return 1))))

So in other words... return 5 * 4 * 3 * 2 * 1

I hope this answers your question.
Last edited on
What you have here is a recursive function; which returns a factorial number. So when "5" is put in, it would return 120 because the function does the following:

5*4*3*2*1

Because when x != 1, it would return the function again using a x-1 as the perimeter. Once x does equal 1, it would return the 1 which would be at the end of all values being multiplied.

factorialFinder(5) :: return 5 * (factorialFinder(5-1) :: return 4 * (factorialFinder(4-1) :: return 3 * (factorialFinder(3-1) :: return 2 * (factorialFinder(2-1) :: return 1))))

So in other words... return 5 * 4 * 3 * 2 * 1

I hope this answers your question.


So when it returns 5 * (factorialFinder(5-1) it doesn't do the calculation of '5*(5-1)' (which is 20).

To be honest I still don't understand (and I feel really stupid) but either way I think you answered the question so I'm going to mark this as solved.

I think after reading this 100 times I might finally get it.
So when it returns 5 * (factorialFinder(5-1) it doesn't do the calculation of '5*(5-1)' (which is 20).


That's correct. When you call the function again, it would just redo the function until it hits the "return 1". So, when you do this:

factorialFinder(5)


It would return the 5 * factorialFinder(5-1) --> a.k.a factorialFinder(4).
So:
factorialFinder(5) returns 5 * factorialFinder(5-1) --> a.k.a factorialFinder(4)
factorialFinder(4) returns 4 * factorialFinder(4-1) --> a.k.a factorialFinder(3)
factorialFinder(3) returns 3 * factorialFinder(3-1) --> a.k.a factorialFinder(2)
factorialFinder(2) returns 2 * factorialFinder(2-1) --> a.k.a factorialFinder(1)
factorialFinder(1) returns 1

So in other words: 5 * 4 * 3 * 2 * 1.

I hope this makes it clearer.
Last edited on
Topic archived. No new replies allowed.