0

i am passing values to function by variable but i am getting inside the function a different values.

float MATH_Pow(float base,int exponent) {
    float value=1;
    if ( exponent > 0 ){
        while (exponent != 0)
        {
            value *= base;
            --exponent;
        }
        return value;
    } else if (exponent < 0){
        while (exponent != 0)
        {
            value *= base;
            ++exponent;
        }
        return 1/value;
    } else if (exponent == 0){
        return value;
    }
}

this is how i am calling the function.

int base=5;
int Exponent=2;
result = MATH_Pow(base,exponent)

but when i am Debugging i am finding different values like 2342345499 for the base and exponent paramenters something not logic.

thanks for your help.

Clifford
  • 88,407
  • 13
  • 85
  • 165
  • 1
    make sure that you declare `float MATH_Pow(float base,int exponent);` in the file calling the function. – Jean-François Fabre Mar 08 '18 at 16:27
  • 1
    Typo: `else if (exponent > 0){` --> `else if (exponent < 0){` – 001 Mar 08 '18 at 16:28
  • Please give a [mcve] which shows *what* inputs give a problem. I don't see how the bug you have is related to values mysteriously changing when passed to your function. – John Coleman Mar 08 '18 at 16:29
  • 1
    Shouldn't `} else if (exponent > 0){` be `} else if (exponent < 0){`? You already have the case `if ( exponent > 0 )` ... – AntonH Mar 08 '18 at 16:29
  • the typos are another issue when exponent is negative, but the main issue is forgetting the function prototype (since in OP test exponent is positive) – Jean-François Fabre Mar 08 '18 at 16:30
  • I don't understand the duplicate (though I think it should be closed as a typo). What happens here is the *opposite* of the duplicate question. In that question floats are being passed to an int parameter. Here ints are being passed to a float parameter, which is a non-problematic thing to do. – John Coleman Mar 08 '18 at 16:33
  • yes it is typo mistake – Mohammad Omari Mar 08 '18 at 16:37
  • what is Happening. i am calling the function and passing two Parameters MATH_Pow(base,exponent) , but while i am Debugging inside the function itself i am finding that the values that are passed to the function are random values from the Memory not the same values that i passed them – Mohammad Omari Mar 08 '18 at 16:39
  • @JohnColeman Going in either direction is fine (although the `float` to `int` case causes truncation). The problem is when there's no declaration, so the compiler can't do the implicit conversion. – dbush Mar 08 '18 at 16:40
  • @MohammadOmari Then you probably don't have a proper prototype for the function. See the linked duplicate. – dbush Mar 08 '18 at 16:41
  • @dbush but here the function *declaration* specifies all types. Perhaps OP has an inconsistent prototype somewhere else. It is hard to tell without a [mcve]. In any event, this is a question that should be closed for multiple reasons, so it doesn't matter all that much. – John Coleman Mar 08 '18 at 16:46
  • how can i add a correct prototype, should i add it in the Header file of my code , because i am trying to build math library for my sytem. and what is a correct prototype for this case – Mohammad Omari Mar 08 '18 at 16:46
  • @MohammadOmari The function prototype should match the definition (i.e. the same as the definition but without the body), and it should go in your header file. – dbush Mar 08 '18 at 16:49
  • i try it and it didnt work.also i tried to use pointers and it works fine with pointers but i dont see it required for this simple function call. – Mohammad Omari Mar 08 '18 at 16:51

0 Answers0