What is Recursion | Recursive Function

Recursive function is a function that calls itself.

 

Let’s look at the following example,

 

using System;

namespace ConsoleApp12 {
    class Program {
        static void Main(string[] args) {
            var factorialOf = factorial(5);
            Console.WriteLine(factorialOf);
            Console.ReadKey();
        }

        public static int factorial(int input) {
            if (input == 1 || input == 0)
                return 1;
            else
                return input * factorial(input - 1);
        }
    }
}

 

Output: 120

 

 

5 2 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x