Recursion

Recursion is the process of a function calling itself, and the function that is doing it is referred to as a recursive function. A base condition and a recursive condition make up the recursive function.

Recursive functions need a base case to ensure that the recursion ends; otherwise, they will run indefinitely, which is not what you want. The basic case is the scenario in which the function doesn’t recur. The base case, for instance, is when we attempt to use recursion to get the factorial of a number and our number ends up being less than 1.

The formula used to get a number’s factorial is an illustration of a recursive function.

int factorial(int n){
    if (n == 0 || n == 1){
        return 1;
    }
    return n * factorial(n - 1);
}
Shubhajna Rai
Shubhajna Rai

A Civil Engineering Graduate interested to share valuable information with the aspirants.

Leave a Reply

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

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in