Recursive function

I'm trying to write a function to compute 3^2 + 3^3 + ...+ (3^n)
Don't get why its saying I have too many arguments:

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

    #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);
    }

Last edited on
well, if you notice, your function, func, takes two arguments, and you only pass it one
what do you mean? / How do I fix it then?
Last edited on
pass it two arguments?
Like this or something: ?

1
2
3
4
5
6
7
8
9
10
  
 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) + function(base, power + 1);
    }
Last edited on
Like this? I'm still getting an error though, help plz.

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
#include <iostream>
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==1)
		 return 9;
	  else
		 return pow(base, power) + func(base, power + 1);
 }
I'm getting LNK 1561: entry point must be defined error. How do I fix this??

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
#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);
 }
1
2
3
4
5
6
//Function prototype
int func(int);

// ...

int func(int base, int power)


These are 2 functions with different parameters.
func(int) is defined nowhere, but func(int, int) is.

You have 2 options
1. write the implementation of the function like this:
1
2
3
4
int func(int power)
{
   ...
}


2. write the decleration of the function like this and use it correctly in main
1
2
3
4
5
6
7
8
9
10
11
//Function prototype
int func(int, int);

// ...

int main(void)
{
   // ... 
   cout << func(3, number);
   // ...
}

that is not the only issue. he is still passing func one number
@Dkob1
copy and paste your error here.

The problem you are telling us (LNK1561) does not match your code.
http://cpp.sh/6qjt

COPY the real error, dont try to type it in.

Error 1 error LNK1561: entry point must be defined


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
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
    
//Function prototype
int func(double n);
    
int main()
{
double n;
cout << "Enter an integer value: ";
cin >> n;    	 
cout << "The function of " << n << "is";
cout << func(n) << endl;
  
	system("pause");
    return 0;
}
    
double func(double n)
 {  
      if(n <= 2)
         return pow(3, n);
	  else
		 return pow(3, n) + pow(3, n - 1);
 }
Last edited on
Look closer.

int func(double n);

double func(double n)

You prototype it as integer, but define it as double.
[quote]@Dkob1
copy and paste your error here.

The problem you are telling us (LNK1561) does not match your code.
http://cpp.sh/6qjt

COPY the real error, dont try to type it in.[quote]
uhhh it completely matches with his code.
@Little Bobby Tables
uhhh it completely matches with his code.

No it doesnt, error LNK1561 refers to main() missing.
https://msdn.microsoft.com/en-us/library/ky737ya4.aspx

his code generates the following errors...
1
2
3
4
c:\users\me\qqq.cpp(27): error C2556: 'double func(double)' : overloaded function differs only by return type from 'int func(double)'
c:\users\me\qqq.cpp(12) : see declaration of 'func'
c:\users\me\qqq.cpp(27): error C2371: 'func' : redefinition; different basic types
c:\users\me\qqq.cpp(12) : see declaration of 'func'

Last edited on
@Jaybob66 Yep, you're 100% right. I got the same errors running it, not a LNK1561. And In my post which is 3 posts above this one, I provided the solution.
Last edited on
@TarikNeaj
Yes, your solution is the solution to his code issues,

But the error he reports makes me think that he has other problems, like a project setting wrong or something. OP makes no point about overloading/redefinition errors (c++ problems)
Only the linker one.
Topic archived. No new replies allowed.