Recursive Functions in C#

Question: What is a recursive function in C#? Give an example.

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

A function that calls another function is normal but when a function calls itself then that is a recursive function. Let's understand with an example how to calculate a factorial with and without recursion.

First we calculate without recursion (in other words, using iteration).

Step 1: Create a console application named InterviewQuestionPart4.

console application

Step 2: First we create a method for the calculation of the factorial and make a static method to invoke the method directly without using the instance of the class with the following code.

  1. using System;    
  2. namespace InterviewQuestionPart4    
  3. {    
  4.     class Program    
  5.     {    
  6.         static void Main(string[] args)    
  7.         {    
  8.             Console.WriteLine("Please Enter a Number");    
  9.     
  10.             //read number from user    
  11.             int number =Convert.ToInt32(Console.ReadLine());    
  12.     
  13.             //invoke the static method    
  14.             double factorial = Factorial(number);    
  15.     
  16.             //print the factorial result    
  17.             Console.WriteLine("factorial of"+number+"="+factorial.ToString());    
  18.     
  19.         }    
  20.         public static double Factorial(int number)    
  21.         {    
  22.             if (number == 0)    
  23.                 return 1;    
  24.     
  25.             double factorial = 1;    
  26.             for (int i = number; i >= 1;i-- )    
  27.             {    
  28.                 factorial = factorial * i;    
  29.             }    
  30.             return factorial;    
  31.         }    
  32.     }    
  33. }   
Now see the output.

output

Step 3: Now for how to convert this function into a recursive function, for example if we want to calculate the factorial of 4, there are two methods like.

recursive function

So we will calculate the factorial like this.

4!=4x(4-1)x(4-2)x(4-3)=24

In other words, the Factorial method will call itself by the following code.
  1. using System;    
  2.     
  3. namespace InterviewQuestionPart4    
  4. {    
  5.     class Program    
  6.     {    
  7.         static void Main(string[] args)    
  8.         {    
  9.             Console.WriteLine("Please Enter a Number");    
  10.     
  11.             //read number from user    
  12.             int number =Convert.ToInt32(Console.ReadLine());    
  13.     
  14.             //invoke the static method    
  15.             double factorial = Factorial(number);    
  16.     
  17.             //print the factorial result    
  18.             Console.WriteLine("factorial of"+number+"="+factorial.ToString());    
  19.     
  20.         }    
  21.         public static double Factorial(int number)    
  22.         {    
  23.             if (number == 0)    
  24.                 return 1;    
  25.             return number * Factorial(number-1);//Recursive call    
  26.     
  27.         }    
  28.     }    
  29. }    
So by the following code the first time we pass the number 4 and it will check, if the condition is zero then come to the next number 4 to multiply the number (4-1) when the function is called. So we are parsing a different value for this parameter 4*3 and the function calls itself again, then for 4*3*2 the function calls itself again and then it checks whether that number is equal to zero and if it is then it breaks the recursion and control is returned to whoever called the function.

Now we will run the program and see the expected output for factorial of 5, it is the same but now it is the recursive function that calls itself.

program output

 

Up Next
    Ebook Download
    View all
    Learn
    View all