473,405 Members | 2,349 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

simple question regarding fact function

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?
Nov 26 '05 #1
7 1544

"Paminu" <ja******@asd.com> wrote in message
news:dm**********@news.net.uni-c.dk...
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
Nov 26 '05 #2
Paminu wrote:
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.
Nov 26 '05 #3
Skarmander wrote:
Paminu wrote:
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.

I have done that, but the last thing it does is return 1, it does not enter
the else statement.
Nov 26 '05 #4
Paminu wrote:
Skarmander wrote:

Paminu wrote:
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.


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.
Nov 26 '05 #5
Paminu wrote:
Skarmander wrote:

Paminu wrote:
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.


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;
}

--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 26 '05 #6
Paminu wrote:

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).

--
Thad
Nov 26 '05 #7
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

em********@gmail.com

Nov 27 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: JKop | last post by:
Take the following simple function: unsigned long Plus5Percent(unsigned long input) { return ( input + input / 20 ); } Do yous ever consider the possibly more efficent:
51
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct...
18
by: Q. John Chen | last post by:
I have Vidation Controls First One: Simple exluce certain special characters: say no a or b or c in the string: * Second One: I required date be entered in "MM/DD/YYYY" format: //+4 How...
7
by: Raphael Gluck | last post by:
Hi I'm really new to programming and i'm reading through a book, teach yourself asp.net by SAMS. I'm having some difficulty with one of the vb.net lessons. What i am supposed to be doing is...
7
by: sam_cit | last post by:
Hi Everyone, I have a function declared as inline and i was wondering as to how it would be expanded by the compiler sample(int x) { if (x>0) sample(--x); printf("%d",x); }
30
by: galiorenye | last post by:
Hi, Given this code: A** ppA = new A*; A *pA = NULL; for(int i = 0; i < 10; ++i) { pA = ppA; //do something with pA
10
by: somenath | last post by:
Hi All, I have one question regarding return value cast of malloc. I learned that we should not cast the return value of malloc because it is bug hider. But my question is as mentioned...
1
by: bg_ie | last post by:
I'm designing a database with 3 tables called Function, Test and Scene. A Function has multiple Tests, but a Test has only one Function. A many to many relationship exists between Test and Scene...
25
by: Jeremy Banks | last post by:
Hi. I wondered if anyone knew the rationale behind the naming of the Popen class in the subprocess module. Popen sounds like the a suitable name for a function that created a subprocess, but the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.