The Cost of Recursion

By Jon Bentley

Dr. Dobb's Journal June 1998

(a)
int istrlen(char *s)
{   int n = 0;
    for ( ; *s; s++)
        n++;
    return n;
}
(b)
int rstrlen(char *s)
{   if (*s) return 1+rstrlen(s+1);
    else return 0;
}

Example 1: (a) Typical implementation of strings; (b) implementing the function recursively.

Back to Article


Copyright © 1998, Dr. Dobb's Journal