simple question regarding fact function

P

Paminu

I have this code (factorial function):

int fact (int n)
{
if (n < 1)
{
return 1;
}
else
{
return (n * fact(n-1));
}
}

int main()
{
printf("result: %d\n",fact(3));
return 0;
}

When I run the program the result is: 6. But at some point n = 0 and (n<1)
becomes true and therefore "return (1)" is executed. How can the result be
6 (which is correct) when the last thing this function does is return 1?
 
R

rayw

Paminu said:
I have this code (factorial function):

int fact (int n)
{
if (n < 1)
{
return 1;
}
else
{
return (n * fact(n-1));
}
}

int main()
{
printf("result: %d\n",fact(3));
return 0;
}

When I run the program the result is: 6. But at some point n = 0 and (n<1)
becomes true and therefore "return (1)" is executed. How can the result be
6 (which is correct) when the last thing this function does is return 1?

Coz you're using recursion, and the 'return 1' returns to the previous call
to fact (fact(0))

fact(3) = 3 * fact(2)
fact(2) = 2 * fact(1)
fact(1) = 1 * fact(0)
fact(0) = 1

so, as it unwinds ...

1 * 1 = 1
2 * 1 = 2
3 * 2 = 6
 
S

Skarmander

Paminu said:
I have this code (factorial function):

int fact (int n)
{
if (n < 1)
{
return 1;
}
else
{
return (n * fact(n-1));
}
}

int main()
{
printf("result: %d\n",fact(3));
return 0;
}

When I run the program the result is: 6. But at some point n = 0 and (n<1)
becomes true and therefore "return (1)" is executed. How can the result be
6 (which is correct) when the last thing this function does is return 1?

Write out the invocation.

fact(3):

if (3 < 1) {
return 1;
} else {
return (3 * fact(3 - 1));
}

And so on: 3 * fact(3 - 1) = 3 * 2 * fact(2 - 1) = 3 * 2 * 1 * fact(1 -
1) = 3 * 2 * 1 * 1 = 6.

S.
 
P

Paminu

Skarmander said:
Write out the invocation.

fact(3):

if (3 < 1) {
return 1;
} else {
return (3 * fact(3 - 1));
}

And so on: 3 * fact(3 - 1) = 3 * 2 * fact(2 - 1) = 3 * 2 * 1 * fact(1 -
1) = 3 * 2 * 1 * 1 = 6.

S.


I have done that, but the last thing it does is return 1, it does not enter
the else statement.
 
S

Skarmander

Paminu said:
Skarmander wrote:





I have done that, but the last thing it does is return 1, it does not enter
the else statement.

I don't know how to explain it, other than that you're looking at it the
wrong way. The last thing done is *not* the "return 1". Here's what's
evaluated in pseudo-syntax:

fact(3)
+----------------------------------------------
fact(2)
+----------------------------------
fact(1)
+----------------------
fact(0)
+----------
return 3 * (return 2 * (return 1 * (return 1)))


(You need a proportional font to view this properly.)

The "return 1" is the last call in the chain; then the final outcome is
evaluated by backtracking through all the calls back to the top,
multiplying the results.

S.
 
M

Michael Mair

Paminu said:
Skarmander wrote:



I have done that, but the last thing it does is return 1, it does not enter
the else statement.

You are going at this from the wrong side.
The last _call_ is to fact(0) but the last return is that of the
call to fact(3).

Note that
return n * fact(n-1);
is the same as
int r = n * fact(n-1);
return r;
With that knowledge, we can instrument your code with printf()s:

#include <stdio.h>

int fact (int n)
{
printf("fac(%d)\n",n);
if (n < 1)
{
printf(" innermost\n");
return 1;
}
else
{
int r = n * fact(n-1);
printf(" n=%d, r=%d\n", n, r);
return r;
}
}

int main (void)
{
printf("result: %d\n",fact(3));
return 0;
}
 
T

Thad Smith

Paminu said:
I have this code (factorial function):

int fact (int n)
{
if (n < 1)
{
return 1;
}
else
{
return (n * fact(n-1));
}
}

int main()
{
printf("result: %d\n",fact(3));
return 0;
}

When I run the program the result is: 6. But at some point n = 0 and (n<1)
becomes true and therefore "return (1)" is executed. How can the result be
6 (which is correct) when the last thing this function does is return 1?

The function fact() is called recursively. In the program, fact() is
called 4 times: fact(3), fact(2), fact(1), and fact(0). The result
that is returned to main() is that of fact(3), which isn't the _last_
invocation of fact(). It is important to keep track of the separate
invocations of fact().

fact(3) will return 3 * fact(2). That will be the final result
returned to main(), not the results of fact(0).
 
E

emailvamsi

hello paminu i am vamsi ,

the mistake which u have made is u forgot that when
a condition is failed in a construct the preceeding statements will not
get executed , so here when n=0 or n<1 is happening means the control
does not go to the statement return(1); bcoz the condition is not
satisfied . i hope u understood. for more clarification contact me at

(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top