Realistic Example of Recursive Function

Before reading this article, I highly recommend reading the previous part:

Question: What is a realistic example of the use of recursion?

Answer: A recursive function is a function that calls itself.

Probably in an interview, an interviewer may ask you to provide an example of a use of recursion in your project.

You may answer, there is no project where the requirement is to find all the files in a folder and in all the sub-folders in the hierarchy. This is a very good example of where we could use recursion.

For example we have a folder structure and in this one folder sample it has 2 files and one folder, then the outer folder has 2 files and one folder, InnerFolder1, has again 2 files and one folder, InnerFolder2, is the innermost folder with only two files, no other folders, like in the following image.


So basically we want a method that can loop through the folder hierarchy and provide us all the file names. We can easily do that using a recursive function.

So, we will first create the method by the following procedure.

Step 1

Create a console application named InterviewQuestionPart5.

console application

Now create the method for finding and printing all the file names from the folder structure by the following code.

  1. using System;    
  2. namespace InterviewQuestionPart5    
  3. {    
  4.     class Program    
  5.     {    
  6.         static void Main(string[] args)    
  7.         {    
  9.         }    
  10.         private static void FindFiles(string path)    
  11.         {    
  12.             foreach(string filename in Directory.GetFiles(path))    
  13.             {    
  14.                 Console.WriteLine(filename);    
  16.             }    
  18.             foreach (string directory in Directory.GetDirectories(path))    
  19.             {    
  20.                 //FindFiles is calling itself    
  21.                 FindFiles(directory);    
  23.             }    
  25.         }    
  26.     }    
  27. }    

Code description

Now what we are doing in this method is first we get part of the folder structure by the parameter path in the method and now there are two foreach loops, the first loop invokes the GetFiles() static method and gets us all the files that are in the folder only files, not folders, and prints all the file names. After printing a filename it comes out to the first loop and goes to the next loop. What we are doing is invoking the GetDirectories() static method that gives us the directory in the folder. Now we are calling the FindFiles() method that is the same method, it is calling itself. Now it will the first foreach loop again, it is calling the GetFiles() method and prints the file name in the OuterFolder. And again, it will executer the second foreach loop and prints the inner directory in the OuterFolder and we are calling itself again, FindFiles(). So it will do like this until it is common to the innermost folders and prints the files that are in that folder from the first loop. Now when we get to the second loop it calls the GetDirectories() and it's not going to return any directory. At this point the method doe not return, so the recursion will break and control is returned to the caller.

Step 2

Now to implement this static method we need to include the namespace System.IO. And we get the path of the folder structure from the user then invoke the static method without using a class name by the following code.

  1. using System;    
  2. using System.IO;    
  4. namespace InterviewQuestionPart5    
  5. {    
  6.     class Program    
  7.     {    
  8.         static void Main(string[] args)    
  9.         {      
  10.            //get path from user    
  11.             Console.WriteLine("please enter Path");    
  12.             string path = Console.ReadLine();    
  14.            //invoke the static method    
  15.             FindFiles(path);    
  17.         }    
  18.         private static void FindFiles(string path)    
  19.         {    
  20.             foreach(string filename in Directory.GetFiles(path))    
  21.             {    
  22.                 Console.WriteLine(filename);    
  24.             }    
  26.             foreach (string directory in Directory.GetDirectories(path))    
  27.             {    
  28.                 //FindFiles is calling itself    
  29.                 FindFiles(directory);    
  31.             }    
  33.         }    
  34.     }    
  35. }    

Step 3

Before running this program, we need to create a folder structure in our system. First we will create the folder structure in a specific path, here I will create a folder structure in the C directory. Inside I will create a folder Sample and within this is file1, file2 and OuterFolder. Then inside it InnerFolder1 and file3 and file4, the same as the Folder Structure.

Folder Structure

Folder image

Step 4

So now we will run the program and provide the specific path and see the output. It shows all the files file1, file2 through to file8 and that is the structure. So this is a very simple example of where we can use recursion.

use the recursion
specific path