Home > C# > C# Recursive Function

As we know that a function or method can be called anywhere in a program depending on its access specifier. Hence, a method can call itself. This process is called recursion and a function performing this process is known as a recursive function. A function which is in the process of potential function calls is also a recursive function. C# allows us to use a function recursively. This is also referred as c# recursion. If a function calls itself then we call this type of recursion as immediate recursion.

How to write a recursive function in C#?

We can write a C# recursive function as:

Return-type Function-Name(argument(s))
{
     if a simple case //this is known as stopping condition.
     {
          return simple value
     }
     else
     {
          Call to function itself with simpler version of problem
     }
}

Below example will clarify the concept of C# recursion.

C# Recursion Example

using System;

namespace recursive_function
{
    class Program
    {
        public int sum(int no)
        {
            if(no == 0)
            {
                return 0; 
            }
            else
            {
                return no + sum(no - 1);
            }
        }
        static void Main(string[] args)
        {
            int no = 6;
            Program p = new Program();
            Console.WriteLine("Sum = "+p.sum(no));
        }
    }
}

Output
C# recursion Example

In the example above we use a C# recursive function to find the sum of all natural number up to the value which we pass to the function. As it is clear that function is calling a simpler version of itself each time the condition is not true. And at the end when the condition is true it executes statement inside stopping condition body.

Let’s have a look at another example to calculate factorial of a number using a recursive function in C#.

using System;

namespace recursive_function
{
    class Program
    {
        public int factorial(int no)
        {
            if(no == 0 || no ==1)
            {
                return 1; 
            }
            else
            {
                return no * factorial(no - 1);
            }
        }
        static void Main(string[] args)
        {
            int no = 0;
            Console.WriteLine("Please enter a positive number : ");
            no = Convert.ToInt32(Console.ReadLine());
            Program p = new Program();
            Console.WriteLine("factorial = "+p.factorial(no));
        }
    }
}

Output
C# recursive function Example
This above function finds the factorial of a natural number. It works exactly like the example above. I.e. if stopping condition is true it executes the statements inside that condition body otherwise, it calls itself with a simpler version of the problem.

Note:-  These are not the only ways to find the sum of all the natural numbers or to find the factorial of a number. These examples are just to explain the concept of recursive functions. And how we can use these functions according to our needs.

Leave a Reply

Your email address will not be published. Required fields are marked *