google web font

Показаны сообщения с ярлыком humor. Показать все сообщения
Показаны сообщения с ярлыком humor. Показать все сообщения

среда, 10 февраля 2016 г.

A word for recursion

Personally, I think that parents must teach recursion to their children as early as they give them counting rods for the first time. The first lesson about counting must be that every number is the previous number plus one
int getNthNumber(int n) {
    int nthNumber = getNthNumber(n-1) + 1;
    return nthNumber;
}
Of course, they need to define that the first number is zero
int getNthNumber(int n) {
    if (n == 1) // first number is zero by definition
        return 0;
    int nthNumber = getNthNumber(n-1) + 1;
    return nthNumber;
}
And there is a second lesson about recursion, which is that the first number have to have the index of zero, too
int getNthNumber(int n) {
    if (n == 0) // first number is zero by definition
        return 0;
    int nthNumber = getNthNumber(n-1) + 1;
    return nthNumber;
}