Important points about function recursion in C programming




1. It is very slow process.


One problem with function recursion is it creates a function frame in each function call. This makes program very slow. This is main reason to introduce for, while and do-while loop in c event that it is also possible by using function recursion.


2. Nature of function recursion is infinite loop or stack over flow.


Calling of same from its function body lead to infinite loop or stack over flow. But with help of conditional statement if-else, switch-case we can able to stop the loop.


3. It follows LIFO data structure.


In function recursion return value of each recursion is stored in stack. As we know stack is LIFO data structure.


4. We can use break keyword in function recursion.


Keyword break can be use to stop any loop. Since function recursion is no a loop so we can use break, continue keyword.



void main(){

int x,num=1;

clrscr();

x=call(num);

printf("%d",x);

getch();
}
int call(int num){

static int x=0;

if(num<4){

x=x+num;

call(num+1);

}

else

break;

return x;
}


//output: Compiler error

5. We can not use goto to move the control from one function to another function.

2 comments:

Nishith Jain M R said...

You can't use break without loop or switch. That's what the standard says. New compilers even wont let you compile.

Ekansh Pathak said...

Can u please give the whole description for the stack usage for any normal function call?
(CDecl calling convention)