Recursion solves a small problem by calling the same function with a smaller value.

Recursion

recursion.cpp
#include <iostream>

int factorial(int value) {
    if (value <= 1) {
        return 1;
    }
    return value * factorial(value - 1);
}

int main() {
    int value = ;
    int result = factorial(value);

    std::cout << "value=" << value << std::endl;
    std::cout << "factorial=" << result << std::endl;
    return 0;
}
#include <iostream>

int factorial(int value) {
    if (value <= 1) {
        return 1;
    }
    return value * factorial(value - 1);
}

int main() {
    int value = ;
    int result = factorial(value);

    std::cout << "value=" << value << std::endl;
    std::cout << "factorial=" << result << std::endl;
    return 0;
}
#include <iostream>

int factorial(int value) {
    if (value <= 1) {
        return 1;
    }
    return value * factorial(value - 1);
}

int main() {
    int value = ;
    int result = factorial(value);

    std::cout << "value=" << value << std::endl;
    std::cout << "factorial=" << result << std::endl;
    return 0;
}
base case A recursive function needs a base case that stops the chain of calls.