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 called **recursive function**. A function can also be called the recursive function if it is in a potential cycle of function calls.

If a function call itself then this type of recursion is called immediate recursion.

A recursive function is written 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 recursive function

Example Program

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)); } } }

This method is written to find the sum of all natural number up to parameter passed to the method.

As it is clear that function is calling a simpler version of itself each time a certain condition is not met. And at the end when a condition is met it executes statement inside stopping condition body.

Another example to calculate factorial of a number is shown below.

Example program

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**

This method is written to find the factorial of a natural number.

This method works exactly like the example above if stopping condition is met it executes the statements inside that condition body otherwise, it calls itself with a simpler version of the problem.