Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

candidate function not viable: expects an l-value for 3rd argument

Calculate nth power of P (both p and n are positive integer) using a recursive function myPowerFunction(int p, int n, int &currentCallNumber). currentCallNumber is a reference parameter and stores the number of function calls made so far. myPowerFunction returns the nth power of p.

int myPowerFunction(int p, int n, int &z)
{
    z++;
    if(n==1)return p;
    else if(n==0)return 1;
    else if(n%2==0)return myPowerFunction(p,n/2,z)*myPowerFunction(p,n/2,z);
    else return myPowerFunction(p,n/2,z)*myPowerFunction(p,n/2,z)*p;
}

int main()
{
    cout << myPowerFunction(3,4,1);
}
like image 772
Rajat Patel Avatar asked Feb 23 '16 20:02

Rajat Patel


1 Answers

You need a variable to pass as the third argument in main_program. You can't pass a constant as a non-const reference.

int count = 0;
std::cout << myPowerFunction(3, 4, count) << 'n';
std::cout << count << '\n';
like image 132
Pete Becker Avatar answered Nov 07 '22 01:11

Pete Becker