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.

recursive function in C#

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.

using System;    
namespace InterviewQuestionPart4    
{    
    class Program    
    {    
        static void Main(string[] args)    
        {    
            Console.WriteLine("Please Enter a Number");    
    
            //read number from user    
            int number =Convert.ToInt32(Console.ReadLine());    
    
            //invoke the static method    
            double factorial = Factorial(number);    
    
            //print the factorial result    
            Console.WriteLine("factorial of"+number+"="+factorial.ToString());    
    
        }    
        public static double Factorial(int number)    
        {    
            if (number == 0)    
                return 1;    
    
            double factorial = 1;    
            for (int i = number; i >= 1;i-- )    
            {    
                factorial = factorial * i;    
            }    
            return factorial;    
        }    
    }    
}

Now see the output.

recursive function in C#

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 in C#

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.

using System;    
    
namespace InterviewQuestionPart4    
{    
    class Program    
    {    
        static void Main(string[] args)    
        {    
            Console.WriteLine("Please Enter a Number");    
    
            //read number from user    
            int number =Convert.ToInt32(Console.ReadLine());    
    
            //invoke the static method    
            double factorial = Factorial(number);    
    
            //print the factorial result    
            Console.WriteLine("factorial of"+number+"="+factorial.ToString());    
    
        }    
        public static double Factorial(int number)    
        {    
            if (number == 0)    
                return 1;    
            return number * Factorial(number-1);//Recursive call    
    
        }    
    }    
}

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


Similar Articles