1

I need help with understanding this code using recursion.

    int power(int n1,int n2);
    int main()
    {
        int base, exp;
        printf("Enter base number: ");
        scanf("%d",&base);
        printf("Enter power number(positive integer): ");
        scanf("%d",&exp);
        printf("%d^%d = %d", base, exp, power(base, exp));
        return 0;
   }
   int power(int base,int exp)
   {
       if ( exp!=1 )
       return (base*power(base,exp-1));
   }

The code works fine but I don't understand why it still manages to return a value when exp is 1 even when the condition in the function body evaluates to false, so I think it shouldn't return anything then.

For example, if the function is called in the main() function and I assign the values of 5 and 1 to base and exp respectively, I expect that I shouldn't get a return value because exp is 1 and my function returns something only if exp !=1 and also, there is no else part in the function body to cover the case when exp is 1. I know about C returning garbage values when the return value is not specified but in this case, it returns the right answer and I tested it several times. For example, when I call it in the main function with base=7 and exp=1, it returns 7 as my answer and even with other "base" values, it always returns the base which is the right answer. And that exactly is my source of confusion - How is C managing to return the right answer to me when the IF clause of the function body evaluates to false... I guess I'm missing something here.

3
  • 1
    Your code invokes Undefined Behavior as you don't return an int when exp is 1. The function will return a "random" , "garbage" value from memory. So anything can be the result. Commented Jun 24, 2015 at 7:07
  • It does not return when exp = 1, but if you catch the return value when call the function, it will be junk value from memory Commented Jun 24, 2015 at 7:07
  • I know about C returning garbage values when the return value is not specified but in this case, it returns the right answer and I tested it several times. For example, when I call it in the main function with base = 7 and exp =1, it returns 7 as my answer and even with other "base" values, it always returns the base which is the right answer. And that exactly is my source of confusion - How is C managing to return the right answer to me when the IF clause of the function body evaluates to false... Commented Jun 24, 2015 at 7:21

3 Answers 3

3

Actually your function is Undefined Behaviour, because of is not returning nothing in case of exp == 1

The value you have in return to your main function is something random.

Sign up to request clarification or add additional context in comments.

Comments

1

You may warned by the compiler in the declaration int power(int base,int exp)

No return, in function returning non-void

Hence, you will get undefined behavior, when you call power() with exp = 1.

Note that: Your power() will fall into recursion trap if exp is negative.

Comments

1

In your case, for exp having value 1, there is no return statement. If the return value of that call is used in the caller function, program shows UB.See note below

That said, the issue will also hold true in case you're calling power() with exp value something other than 1, as power() will end up calling itself with exp as 1 at some point of time.

Finally, exp being an int, it can accept a negative value and go into infinite loop, only to create stack overflow by infinite recursion.


Note: As per the C11 standard, if the function is terminated by reaching the closing } and the returned value is used, it invokes invokes undefined behaviour.

Ref: Chapter §6.9.1, paragraph 12,

If the } that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined.

3 Comments

I know about C returning garbage values when the return value is not specified but in this case, it returns the right answer and I tested it several times. For example, when I call it in the main function with base = 7 and exp =1, it returns 7 as my answer and even with other "base" values, it always returns the base which is the right answer. And that exactly is my source of confusion - How is C managing to return the right answer to me when the IF clause of the function body evaluates to false...
@AyorindeKomolafe well, undefined behaviour is undefined. It includes working "as expected" case also.
really? I didn't know that. I thought if it was undefined, it might throw different answers to me each time I run it, but it was giving me consistently right answers.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.