Sluggers,

Thanks lots.

I understand clearly, why 24 instead of 1 is printed, now.

When y is 0 (y<1) finally, the program had stacked up
4*3*2*1 by doing factorial(y) repeatedly which is cool.
Multiply by 1 (return 1, when y<1), 4*3*2*1*1 gives
24. Very, very cool.

I had seen the stacking up with gdb program, which I
learned with no hassles. Now, I can't do without it in my
C programming lessons.

I got two birds so to say with one stone.

And with no flames, too, at all in the postings. Everyone is
professional.

Thanks a lot.

Beav

On 11/27/05, Beav Petrie <[EMAIL PROTECTED]> wrote:
Sluggers,

I run these codes I copied from a tutorial book.
The print out is 24, correct factorial of 4 (4*3*2*1).

But y is 0 (y < 1) finally and return value of 1 so how is it
24 instead of 1 is printed ? 

Please help me understand. Many thanks.

Do not flame me, please. I am a newbie.

Beav


#include <stdio.h>

int factorial(int y)
{
            if ( y < 1){
                 return 1;
            }
            else
            {
                  return (y * factorial(y - 1));
             }
}

int main(void)
{
             printf("Factorial result: %d\n",factorial(4));
             return 0;
}

-- 
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html

Reply via email to