+ 2
What is the concept of Recursion??
7 Answers
+ 3
Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function.
void recursion()
{
recursion(); /* function calls itself */
}
int main()
{
recursion();
}
The C programming language supports recursion, i.e., a function to call itself. But while using recursion, programmers need to be careful to define an exit condition from the function, otherwise it will go into an infinite loop.
hope this helps :)
+ 7
Recursion is calling a method by itself inside its definition. The concept allows you to solve an algorithm by repetitively calling this method, while executing it "by-hand" only once.
Recursion, though, for it not to become an infinite loop has to have a so-called "base case" inside it. Most of the time it is a condition based on the method's argument that make the method return a fixed value.
Recursion allows you to tackle complex algorithms with little code. That comes for a price, though. It is relatively slow and memory-consuming. Theoretically, every problem that can be done with recursion, can also be solved without it (looping and iffing only).
+ 2
Thanks... :-)
+ 1
Definition : Calling a method by itself inside it's block definition is called recursion.
It is also used for iteration like Looping statements.
we can use it with or without basecase.
WITHOUT BASECASE: It is just like while loop having always a TRUE condition. in it.
void Foo() {
while(true){ /* do something */ }
}
OR
void Foo(){
//do something
Foo();
}
WITH BASECASE: basically it is used when you don't know the iteration limit for an operation/step/statement(s), but you know when it should be stopped (that condition is called basecase).
void recursion (){
// do something
if(basecase_condition) recursion ();
}
int main(){
recursion ();
}
Although it is just as like as while loop in programming,
so you can say that it is just another way to write while loop in this way, and so called RECURSION introduced.
0
Just adding to the excellent description of recursion by @kuba, the idea of dynamic programming will help in making recursion more efficient.
Dynamic programming uses the technique of memoization to avoid repititive function calls of the same values which makes recursion slow and inefficient.
The alternative bottom-up approach also does similar improvement.
0
Read my next post.
0
Read my previous post.