#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>

using namespace std;

double power(double b, double p);


void main(void){

    int base = 0, power = 0, result = 0;
    cout << "Programmed by ." << endl << endl;

    cin >> base;
    cout << " ^ ";
    cin >> power;
    result = power(base, power);
}

double power(double b, double p){
//b = base, p = power
    double equation, result;
    if (p == 1){
        return b;
    }
    if (int(p) % 2 == 0){
        // (b^2)^(p/2)
        equation = (pow(pow(b, 2), (p / 2)));
        result = equation * power(b, p - 1);
    }
    if (int(p) % 2 == 1){
        // b * (b^2)^((p-1)/2)
        equation = b * (pow(pow(b, 2), ((p - 1) / 2)));
        result = equation * power(b, p - 1);
    }
}

Think its self explanatory on what its supposed to do, but i dont know how to return to main, because its being called from the function by the time it reaches the return value. Some help would be greatly appreciated!

Think its self explanatory on what its supposed to do, ....

It's only self explanatory if it's commented, therefore it's not. We have to figure it out... ;)

but i dont know how to return to main, because its being called from the function by the time it reaches the return value.

It's called from main() the first time, so the last return gets to main() For example:
call sequence starts
main: 1st call to power
1st power: 2nd call to power
2nd power: 3rd call to power
3rd power: 4th call to power
return sequence starts
4th power: returns to 3rd power
3rd power: returns to 2nd power
2nd power: returns to 1st power
1st power: returns to main

ok, the first part in the function is when the power finally gets down to 1 is when it returns to main(), the second if statement is if the power is even, and the last if is for when the power is odd. We were given numbers to type in for it to calculate out, thats why i just need to enter numbers, and why it is not asked what the numbers are.

Its main function is to compute the exponentiation of some base to a given power.

#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>

using namespace std;

double power(double b, double p);


void main(void){

    int base = 0, power = 0, result = 0;
    cout << "Programmed by Paul Jones." << endl << endl;

    cin >> base;
    cout << " ^ ";
    cin >> power;
    result = power(base, power);
    cout << " = " << result;
}

double power(double b, double p){
//b = base, p = power
    double equation, result;
    if (p == 1){
        return b;
    }
    if (int(p) % 2 == 0){
        // (b^2)^(p/2)
        equation = (pow(pow(b, 2), (p / 2)));
        //result = equation * power(b, p - 1);
        return equation * power(b, p - 1);
    }
    if (int(p) % 2 == 1){
        // b * (b^2)^((p-1)/2)
        equation = b * (pow(pow(b, 2), ((p - 1) / 2)));
        //result = equation * power(b, p - 1);
        return equation * power(b, p - 1);
    }
}

I changed it up a bit inside the function commenting out what i previously had, but im still getting an error saying "term does not evaluate to a function taking 2 arguments" for line 19

im still getting an error saying "term does not evaluate to a function taking 2 arguments" for line 19

You have both a function and a local variable called power. The local variable hides the function name, and your compiler is telling you that your local variable can't be used as a function. :)

#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>

using namespace std;

double power(double b, double p);


void main(void){

    double base = 0, pow = 0, result = 0;
    cout << "Programmed by ." << endl << endl;

    base = 2;
    pow = 32;
    cout << base << " ^ " << pow << " = ";
    result = power(base, pow);
    cout << result << endl;

    base = 3;
    pow = 3;
    cout << base << " ^ " << pow << " = ";
    result = power(base, pow);
    cout << result << endl;

    base = 2;
    pow = 15;
    cout << base << " ^ " << pow << " = ";
    result = power(base, pow);
    cout << result << endl;
}

double power(double b, double p){
//b = base, p = power
    double equation, result;
    if (p == 1){
        return b;
    }
    if (int(p) % 2 == 0){
        // (b^2)^(p/2)
        equation = (intpow(square(b, 2), (p / 2)));
        result = equation * power(b, p - 1);
        //return equation * power(b, p - 1);
    }
    if (int(p) % 2 == 1){
        // b * (b^2)^((p-1)/2)
        equation = b * (intpow(square(b, 2), ((p - 1) / 2)));
        result = equation * power(b, p - 1);
        //return equation * power(b, p - 1);
    }
}

Revised main to be what it is needed to be to come out right, but im still a bit confused on how to get the function working, =/

Some points:

  • Don't use void main for reasons discussed in the link in my signature.
  • square() is not a function in the standard template library. You'll have to define it yourself.
  • Likewise intpow() has not been declared. I assume it's an integer overload function for the regular pow function.
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>

using namespace std;

double power(double b, double p);


void main(void){

    double base = 0, powr = 0, result = 0;
    cout << "Programmed by ." << endl << endl;

    base = 2;
    powr = 32;
    cout << base << " ^ " << powr << " = ";
    result = power(base, powr);
    cout << result << endl;

    base = 3;
    powr = 3;
    cout << base << " ^ " << powr << " = ";
    result = power(base, powr);
    cout << result << endl;

    base = 2;
    powr = 15;
    cout << base << " ^ " << powr << " = ";
    result = power(base, powr);
    cout << result << endl;
}

double power(double b, double p){
//b = base, p = power
    double equation, result;
    if (p == 1){
        return b;
    }
    if (int(p) % 2 == 0){
        // (b^2)^(p/2)
        // pow(double(y3-y1),2)
        equation = (pow(pow(b, 2), (p / 2)));
        //result = equation * power(b, p - 1);
        return equation * power(b, p - 1);
    }
    if (int(p) % 2 == 1){
        // b * (b^2)^((p-1)/2)
        equation = b * (pow(pow(b, 2), ((p - 1) / 2)));
        //result = equation * power(b, p - 1);
        return equation * power(b, p - 1);
    }
}

Ok, tried fixing it up bit, made a mistake by posting that last one when i knew i had those mistakes in the function, lol

Still giving me the wrong numbers when it prints out. And our instructors havent told us about the int main() yet, so i dont want to get in trouble by using it.

> And our instructors havent told us about the int main() yet
I doubt they even know (or care).
One wonders what other rubbish they're going to teach you based on their broken understanding of the language.

> so i dont want to get in trouble by using it
Or suffer incessant nagging whenever you post your code anywhere on the net.
Just use it and see what they say.
Then show them this - http://www.research.att.com/~bs/bs_faq2.html#void-main
and see what they say.
If they've still got an attitude, then you're learning from the wrong people - are you paying for this course? is it value for money?

This article has been dead for over six months. Start a new discussion instead.