As we know a function can be called by another function to accomplish a specific task ,and even a function can be called by itself and this is calling RECURSION .
In another word when a function called by itself then this method is called RECURSION .
In another word when a function called by itself then this method is called RECURSION .
Before going to learn recursion first of all we should learn what is function and how is it called .
#include<stdio.h>
void next_func() ; // function prototyping
main ()
{
printf("Right now i am in main and now going to another function \n ");
next_func(); // function calling
printf("I am again in main");
}
void next_func() // function definition
{
printf("Now i am in next_func \n ");
}
output :-Right now i am in main and now going to another function
Now i am in next_func
I am again in main
here the function next_func is calling by main . one more thing should be in your mind that when a function is called ,after executing that function the control goes back to the function from where is it called .
Consider on next code segment
#include<stdio.h>
void recursion() //function prototyping
main()
{
printf("This is example of recursion \n ");
recursion(); // function calling
}
void recursion()
{
printf("Now control is in function recursion \n ");
recursion();
}
output :- This is example of recursion
Now control is in function recursion
Now control is in function recursion
.
.
.
.
.
Now control is in function recursion
Infinite times (until the stack is overflow...... will discuss later what is stack overflow )
This is the example of recursion , here function recursion is calling by itself and this is what recursion .
1st main function get execute and print the message " This is example of recursion "
in next line function (recursion ) is calling and control goes to function recursion .....
here the message
in next line function (recursion ) is calling and control goes to function recursion .....
here the message
" Now control is in function recursion" will print and in new line the function called by itself again the message will print and it will continue until the stack will not full .
Now one another question is arising is what is STACK ???
the answer is very simple function calling and returning is done by machine using stack concept ,
when a function is called the address of function is push into stack and when the control returns to another function the address is pop.
in this situation function RECURSION is called again and again so it is pushing in stack again and again ...
so we get message STACK OVERFLOW .
In next article a use of recursion function is given for calculate the factorial of a number .
Now one another question is arising is what is STACK ???
the answer is very simple function calling and returning is done by machine using stack concept ,
when a function is called the address of function is push into stack and when the control returns to another function the address is pop.
in this situation function RECURSION is called again and again so it is pushing in stack again and again ...
so we get message STACK OVERFLOW .
In next article a use of recursion function is given for calculate the factorial of a number .
0 comments: