Visualizing Recursion Function Call - C++ Function

C++ examples for Function:Recursive Function

Description

Visualizing Recursion Function Call

Demo Code

#include <iomanip>
#include <iostream>

unsigned long factorial(unsigned long);

int main(int argc, const char *argv[]) {
    // calculate factorials of 0 through 10
    for (int counter = 0; counter <= 10; ++counter) {
        std::cout << counter << "! = " << factorial(counter) << "\n" << std::endl;
    }//from w w  w  .ja  va2 s .  c om
    return 0;
}
// recursive definition of function factorial
unsigned long factorial(unsigned long number) {
    // base case
    if (number <= 1) {
        std::cout << number << std::endl;
        return 1;
    } else {
        std::cout << number << " * ";
        return number * factorial(number - 1);
    }
}

Result


Related Tutorials