Recursion ?
Recursion means recursion. 🙂
Google explains Recursion in there way, just try searching recursion and see what happens :
So you got the idea now that recursion has something to do with Repetition. Let’s first go throw the formal definition of recursion from Wikipedia :
Recursion is the process of repeating items in a self-similar way.
Recursion in computer science is a method where the solution to a problem depends on solutions to smaller instances of the same problem (as opposed to iteration).[1] The approach can be applied to many types of problems, and recursion is one of the central ideas of computer science.[2]Â – Wikipedia
In programming Language recursive method is the one which calls it self. For example consider the following example :
1 2 3 4 5 6 7 8 9 10 11 12 |
main() { Recursion(5); } void Recursion(int x) { if (x == 0)//base case return; Recursion(x - 1); Console.WriteLine("Recursing"); } |
The above code has a method called Recursion(int x) Â that takes integer x as an argument. This method prints the string “Recursing” x times. I will show you what exactly happens in the stack when you write the above code.
Whenever you call a function, it is added to the call stack. The Function last added to call stack is executed first.
For above code, we will start from main :