4. Recursion
 
C functions may be used recursively; that is, a function may call itself either directly or indirectly.
Recursion may provide no saving in storage, since somewhere a stack of the values being processed    must be maintained. Nor will it be faster. But recursive code is more compact, and often much easier    to write and understand than the non-recursive equivalent. Recursion is especially convenient for    recursively defined data structures like trees.
 
 
int factorial(int n) /* recursive version */
{
    if (n<=1) return 1;
    else return (n*factorial(n-1));
}

int factorial(int n) /* iterative version */
{
    int product = 1;
    for (; n>1; --n) product *= n;
    return product;
}
 
 
 
이전페이지 / 5 / 다음페이지