Search code examples
c++parametersargumentsglobal-variablesparameter-passing

Rewriting a basic program in so that it uses arguments and parameters in place of global variables


I rearranged it so that it fits the requirement however when I run the code it returns a "undefined reference to 'Power (int, int, int)'. I boiled it down to a single line but I do not know how to resolve it. Comparing it to the similar examples I've done it appears to be in order. I believe the problem is that it is not passing the 'return' back to the main.

#include <iostream>

int Power( /* in */ int pow, /* in */ int x, /* out */ int &result);    //prototype

using namespace std;

int main()
{
    int pow;
    int x;
    int result;

    cout << "Enter power: ";    //prompt for integer in for power to be raised by
    cin >> pow;                 //integer in
    cout << "Enter value to be raised to power: ";      //prompt for integer to be raised
    cin >> x;                   //integer in
    cout << Power(pow, x, result);      /*function results output.  returns input of Power function***
                                           This line is the problem but why...
                                           If I remove "(pow,x,result)" it will compile but it does not calculate properly.
                                           If I take the while loop from the Power function by itself it calculates fine.
                                           If I replace Power ((pow,x,result) with 'result' it makes a really big number...
                                         */

    return 0;                   //return
}

int Power( /*in */ int pow, /*in */ int x, /*out */ int result);        //Power function     should calculate the result of raising 'x' to the 'pow' entered...
{
    result = 1;                 //result is set at 1     by default
    while (pow > 0)             //while loop with pow set     to greater than 0
    {
        result = result * x;    //calculation for raising x by pow
        pow--;
    }
    return result;              //returning while loop results to function
}

Solution

  • Why are you passing the result address? I'd suggest you to delete the result variable from the function arguments and the variable in the main scope, this would fix it.

    int power(int pow,int x);
    int main(){
        int pow,x;
        // Get the pow and number and the index, you wrote it
    
        cout << "Enter power: "; //prompt for integer in for power to be raised by 
        cin >> pow; //integer in 
        cout << "Enter value to be raised to power: "; 
        cin >> x;
        cout << Power(pow,x);
    // If you want to store the result do
        int result = Power(pow,x);
    }
    int Power(int pow,int x){
       int result = 1; //result is set at 1 by default , you want to declare result here
    while(pow > 0){
        result = result * x; 
        pow--; } 
    return result;  }
    

    The problem is that you have the definition that says int &result but you declare it as int result. The code above solves your problem and saves you from having to define the variable result and passing it to Power