How do I write a recursive function to compute (3^2)+(3^3)+(3^4).......+(3^n) ?

What have you done, besides posting your homework assignment?Show your code to us and pinpoint the errors you have.We will be more than happy to help. :)

Well said ddanbe.
Here the principle of recursive function :
Technically, a recursive function is a function that makes a call to itself. To prevent infinite recursion, you need to know the stop condition.

Here in your case the stop condition should be n == 1. and you should care …

An other hint?

What could the function prototype look like?

int sumToN( const int nSum, const int nTerm, const int N );

What would the tail recursive call look like?

How would you call the the function?
(What initial values ?)

## All 9 Replies

What have you done, besides posting your homework assignment?Show your code to us and pinpoint the errors you have.We will be more than happy to help. :)

Well said ddanbe.
Here the principle of recursive function :
Technically, a recursive function is a function that makes a call to itself. To prevent infinite recursion, you need to know the stop condition.

Here in your case the stop condition should be n == 1. and you should care about the specif case of n == 0.

An other hint?

What could the function prototype look like?

int sumToN( const int nSum, const int nTerm, const int N );

What would the tail recursive call look like?

How would you call the the function?
(What initial values ?)

not sure but is it something along the line of this?

``````int func(int base, int power)
{   base=3
if(power == 2)
return pow(base, power);
return pow(base, power) + function(base, power - 1);
}
``````

Don't get why its saying I have too many arguments:

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

//Function prototype
int function(int);

int main()
{
int number;
cout << "Enter an integer value and I will display\n";
cout << "the value: ";
cin >> number;

cout << "The function of " << number << "is";
cout << function(number) << endl;

system("pause");
return 0;
}

int func(int base, int power)
{   base=3;
if(power == 2)
return pow(base, power);
return pow(base, power) + function(base, power - 1);
}
``````

Don't call your function, "function" or "func". Name it after what your function does.
You have defined a function prototype which can accept one int parameter, yet you use that function function with two parameters on line 25.

I'm getting error that says, LNK 1561: entry point must be defined error.
How do I fix this, am I doing this right?

``````#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

//Function prototype
int func(int);

int main()
{
int number;
cout << "Enter an integer value and I will display\n";
cout << "the value: ";
cin >> number;
cout << "The function of " << number << "is";
cout << func(number) << endl;

system("pause");
return 0;
}

int func(int base, int power)
{  base=3;
if(power <= 2)
return pow(base, power);
else if (power==3)
return 27;
else
return pow(base, power) + func(base, power + 1);
}
``````

Does line 7:`int func(int);` look the same as line 22:`int func(int base, int power)`?

If you can use an 'helper' calling function then ... HINT:

``````double sumToN( const double nSum, const double x, const double nTerm, const int N )
{
// if( N ...... /// return nSum;
// return sumToN( nSum + /////////////////////
}

// helper calling function ...
double sumToN( const double x, const int N )
{
return sumToN( 0, x, x*x, N );
}

Just fill in the 2 lines and you are done.
``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.