0

I've encountered this strange phenomenon that my "Math.pow" function works wrongly if parameters are given as variable. So note that

int result = pow(2,4);

works just fine. But the following code does not:

int main()
{
    int base = 2;
    int p = 4;

    int result = pow(base,p);

    printf("%i\n" , result);
}

int pow(int base, int pow){
    int result = 1;

    for(int i = 0; i < pow; i++){
        result = result * base;
    }

    return result;
}

Strangely, placing the pow function above main method just solves the problem. Why does this happen?

  • That's how C is specified. A named thing must be declared before you can use it. Declared is not the same as defined though. So you can declare the function before the main and then define it after. – JHBonarius Jan 29 '22 at 17:51
  • There has to be an original of this question we can point to. If you don't declare a prototype for `pow` in advance (or define it before `main`), when compiling `main` the compiler doesn't know what arguments `pow` expects or what it returns. Either define it above `main` or put `int pow(int base, int pow);` (a prototype) above `main` (at least, I think that's the syntax for it -- it's been a lot of years...). – T.J. Crowder Jan 29 '22 at 17:52
  • Or [*Why doesn't the compiler infer function prototypes from function definitions?*](https://stackoverflow.com/questions/25329203/why-doesnt-the-compiler-infer-function-prototypes-from-function-definitions) or several others. – T.J. Crowder Jan 29 '22 at 17:54

1 Answers1

1

The function definition in the second case should be before the function call, as the Compiler needs to know that a function called pow() is there

If you write it afterward the compiler won't know about the function when it gets called.. thus the error

If you want to write the definition afterwards you should declare the function before main()

Something like this -

int pow(int,int); //function declaration before main to let the compiler know

int main()
{
    int base = 2;
    int p = 4;

    int result = pow(base,p);

    printf("%i\n" , result);
}

int pow(int base, int pow){
    int result = 1;

    for(int i = 0; i < pow; i++){
        result = result * base;
    }

    return result;
}

This will help the compiler know that such a function exists and may be defined after main()

Mihir
  • 520
  • 2
  • 6
  • 18
  • Thank you. I didn't know i can declare functions first. However i still do not understand why the problem does not occur when written as "int result = pow(2,4);" :( – Yavuz Karaca Jan 29 '22 at 18:25
  • Are you talking about `Math.pow()` here which gets imported from the math library? OR your own function `pow()` which you have defined over here – Mihir Jan 30 '22 at 03:40
  • Because if you call `Math.pow()` it does not need any definition as it gets imported from the Math library in C which already contains the function definition – Mihir Jan 30 '22 at 03:46
  • Also, maybe try changing the function name from `pow` to something else as C already contains a function called `pow()` inside the Math library.. That might cause issues – Mihir Jan 30 '22 at 03:48