| Start |
|---|
| Recursion in plain English Recursion can be defined as: A method that calls itself until a specific condition is met. An excellent and simple example of recursion is a method that will get the factorial of a given number: In this method, we can see that the method will take an argument, Step by step: Given the example, executing
The key to understanding recursion is that the method calls a new instance of itself. After returning, the execution of the calling instance continues. |
| Fibonacci Sequence You can calculate a number in the Fibonacci sequence using recursion. Following the math theory of F(n) = F(n-2) + F(n-1), for any i > 0, |
| Recursively describe an object structure Recursion is when a method calls itself. Preferably it will do so until a specific condition is met and then it will exit the method normally, returning to the point from which the method was called. If not, a stack overflow exception might occur due to too many recursive calls. |
| Using Recursion to Get Directory Tree One of the uses of recursion is to navigate through a hierarchical data structure, like a file system directory tree, without knowing how many levels the tree has or the number of objects on each level. In this example, you will see how to use recursion on a directory tree to find all sub-directories of a specified directory and print the whole tree to the console. This code is somewhat more complicated than the bare minimum to complete this task, as it includes exception checking to handle any issues with getting the directories. Below you will find a break-down of the code into smaller segments with explanations of each.
The main method takes an input from a user as a string, which is to be used as the path to the root directory. It then calls the
This is the first of two methods that handle the actual directory tree printing. This method takes a string representing the path to the root directory as a parameter. It checks if the path is an actual directory, and if not, throws a
This second method handles the brunt of the work. It takes a Once the current directory is printed, its sub directories are retrieved, and this method is then called on each of them with a depth level value of one more than the current. That part is the recursion: the method calling itself. The program will run in this manner until it has visited every directory in the tree. When it reached a directory with no sub directories, the method will return automatically. This method also catches an The method below provides a more basic approach to this problem: This does not include the specific error checking or output formatting of the first approach, but it effectively does the same thing. Since it only uses strings as opposed to |
| PowerOf calculation Calculating the power of a given number can be done recursively as well.
Given a base number Theoretical Example:
Code Example: Tests in xUnit to verify the logic: |
| Factorial calculation The factorial of a number (denoted with !, as for instance 9!) is the multiplication of that number with the factorial of one lower. So, for instance, 9! = 9 x 8! = 9 x 8 x 7! = 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1. So in code that becomes, using recursion: RemarksNote that using recursion can have a severe impact on your code, as each recursive function call will be appended to the stack. If there are too many calls this could lead to a StackOverflowException. Most "natural recursive functions" can be written as a Always think twice and use recursion carefully - know why you use it:
If you want more theory, please read: |
Read Article
https://develop.horje.com/learn/1434/reference