1. ## Recursion

I am trying to figure out this problem

consider this recurrence relation

f(1)=2
f(2)=2

f(n)=2*f(n-1)+f(-2) for n>2

write a recursive function to compute f.

I have some code that I have been working on. My question is.

How do u get the function to return a value that it computed back to itself to be used again .

When i run this for f(2) and f(1)it comes out right, but for anything else its wrong ( or at least i think.)

can anyone tell mat f(3) and f(4) are so i know what to look for? Also, give me a hint as to what I am doing wrong in the code?

Code:
```#include<iostream>
using namespace std;

int computef(int n)

{

if ((n=2)||(n=1))

return 2;

else

}

int main()
{
int x;

cout << "Please input the number for calculation  " ;
cin >> x;

cout << computef(x);

system("pause");
return 0;

}```

2. How do u get the function to return a value that it computed back to itself to be used again .
Just return the value.

can anyone tell mat f(3) and f(4) are so i know what to look for?
I presume that you have a typographical error and thus f(-2) is actually f(n-2).
f(3) = 2 * f(3-1) + f(3-2) = 2 * f(2) + f(1) = 2 * 2 + 2 = 6

From the above calculation, do you understand how the recurrence relation works now?

EDIT:
Oh wait, the real problem (other than failing to return a value) is that you wrote ((n=2)||(n=1)) instead of ((n==2)||(n==1)).

3. Holy smokes Batman!!!

That worked. So when do u use something like n=2 vs n==2
also, when u use "return" it return the value to the calling function to use it again if the conditions are not met?

revised code.

Code:
```#include<iostream>
using namespace std;

int computef(int n)

{

if ((n==2)||(n==1))

return 2;

else

}

int main()
{
int x;

cout << "Please input the number for calculation  " ;
cin >> x;

cout << computef(x);

system("pause");
return 0;

}```

4. So when do u use something like n=2 vs n==2
The former is used in assignment (or as part of the syntax for initialisation), the latter is used for comparing for equality.

when u use "return" it return the value to the calling function to use it again if the conditions are not met?
Yes. Of course, whether some conditions are met or otherwise does not matter: from the point of view of the function, you are just returning a value.

revised code.
I suggest that you indent your code more consistently. Also remove the cout << answer; since it is not needed and unreachable anyway.

5. Thank you so much foe help. U made my night, i was working on this one problem for like 3 and a half hours