Home > Software design >  Why does function not work when I add a function in it?
Why does function not work when I add a function in it?

Time:05-16

I am new at recursive functions.

int display(int num)  {  
if(num)  {
    display(num-1);
}
else {
    return 0;  
}
printf("\t%d", num);

}

When I run display(5), i can get "1 2 3 4 5" output as i want. BUT: When I add another function in this code and make some changes on the display() function and run the display(5) command it gives no output.. Here is the other code:

int bunnyEars2(int line) {
   if(line == 0) {
       return 0;
   }
         
   if(line % 2 == 1) {
       return 2   bunnyEars2(line-1);
   }
   
   else {
       return 3   bunnyEars2(line-1);
   }
}
int display(int n)  {  
   if(bunnyEars2(n))  {
       display(bunnyEars2(n-1));
   }
   else {
       return 0;  
   }
   printf("\n%d", bunnyEars2(n));
}   

I want to take bunnyEars2(1), bunnyEars2(2), bunnyEars2(3), bunnyEars2(4), bunnyEars2(5) outputs from display(5) command. But it gives no output. Can you help me out with this?

CodePudding user response:

When you call display(5) it calls display(bunnyEars2(4)) which is display(10) => infinite loop

display(n) calls display(bunnyEars2(n-1)) and for every n>=2, bunnyEars2(n-1) > n so that you will get an infinite loop.

CodePudding user response:

Other people have already given the reason for why this code is causing issues so I'll go into how to potentially look the causes of issues like this in the future.

When writing a recursive function, It is important to identify the various cases for your function. Each recursive function will have a set of cases which cause the function to call itself (recursive cases) and a set of functions in which the function will not call itself (base cases). In the case of the display function given in the first example. The recursive case is -- num has a non zero value, and the base case is -- num has a value of 0. A recursive function will only cease recursing if a function call hits a base case causing the recursive function calls to resolve rising up the call stack. Under any circumstance, any call to a recursive function does not lead a call of the function resulting in the execution of the base case, the call to the function will not resolve resulting in infinite recursion.

This infinite recursion is occurring in your second version of the display function. Look closely at the recursive case of your second display function.

if(bunnyEars2(n)){
    display(bunnyEars2(n-1));
}

How would the value of bunnyEars2(n-1) compare to the value of n? Examine whether this recursive case will result in a calling of the function that executes a base case.

If you have access to a debugger and know how to use it, try stepping through the function and see what path it takes through the code. If you do not have access to a debugger, get some paper and a pencil and walk through the execution of your function by hand. This should give you a better idea about what your code is doing and how a problem might arise.

  • Related