You do not need to specify a link as a parameter, as many here indicate. But yes, your input for z cannot be changed since it comes from read-only memory. Treat the input for z as const , internally copy z and pass the copy as a link. Then your desired use works:
int myPowerFunction(int p, int n, const int &z) // z is now const ! { int _z = z + 1; // copy ! 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() { std::cout << myPowerFunction(3, 4, 1); }
source share