473,413 Members | 1,778 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,413 software developers and data experts.

stack operation in recursive function

40
Hello everybody...


I want to know stack operation for recurssive factorilal function..... Means how it exactly returns value and over all calcultion will be carried out...U can follow the below code and explain...

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. int fact(int);
  3. int main()
  4. {
  5. int f,n;
  6. printf("Enter the number whose factorial you want to find..\n");
  7. scanf("%d",&n);
  8. f=fact(n);
  9. printf("Factorial of given number is %d\n",f);
  10. return 0;
  11. }
  12.  
  13. int fact(int n)
  14. {
  15. int f;
  16. if(n==0)
  17. return(1);
  18. else
  19. return(fact(n-1)*n);
  20. }
.Plz let me know...

Thank you,
Manjiri
Feb 7 '07 #1
1 1647
Ganon11
3,652 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. int fact(int n)
  2. {
  3. int f;
  4. if(n==0)
  5. return(1);
  6. else
  7. return(fact(n-1)*n);
  8. }
Suppose the user enters 1. Then the function will enter once and hit the 'else' statement. Now we are finding fact(0) and multiplying it by 1 - in other words, returning simply fact(0). The function is entered again, and this time the 'if' branch is executed - 1 is returned. So fact(1) = fact(0) * 1 = 1 * 1 = 1.

Suppose the user enters 2. Then the function will enter and hit the 'else' statement. You are now finding fact(1) and multiplying it by 2. fact(1) evaluates in the same way as before, so fact(2) = fact(1) * 2 = (fact(0) * 1) * 2 = (1 * 1) * 2 = 2.

This can be generalized to:

fact(n) results in fact(n-1) * n = ((fact(n - 2) * (n - 1)) * n = ... = (((1 * 1) * 2) * 3)... * n.

The function will be executed n + 1 times.
Feb 7 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Allen | last post by:
Hi all, What are some different approaches to dealing with stack overflows in C++? I'm especially interested in approaches concerned with speed for infrequent overflows. -- Best wishes,...
4
by: Victor | last post by:
Hello, I've got a situation in which the number of (valid) recursive calls I make will cause stack overflow. I can use getrlimit (and setrlimit) to test (and set) my current stack size. ...
5
by: ip4ram | last post by:
I am quite puzzled by the stack overflow which I am encountering.Here is the pseudocode //define stack structure //function operating on stack void my_stack_function( function parameters) {...
13
by: Kenneth Lantrip | last post by:
In Ansi-C C99 or Standard C or in GCC... Is there a way to push a value onto the stack and then later retrieve it within the same function? void foo(void) {
13
by: Ben R. Bolton | last post by:
The documentation indicates that the threads "default stack size" is 1MB. The work "default" implies that it can be changed. Is it possible to change the StackSize in .NET? If so how? Is it...
4
by: Jesper Stocholm | last post by:
I have a recursive function that I would like to do a lot of recursive calls (preferebly 2^20 in total) The function is called as (with maxi = e.g. 100000) DoRecursion(0,maxi,bseed,sha); ...
2
by: 63q2o4i02 | last post by:
Hi, I've written a top-down recursive decent parser for SPICE circuit descriptions. For debugging purposes, I wanted each production rule/function to know what its own name was, so at the...
13
by: deepak | last post by:
Hi In the following function how the memory 'll be allocated. 1) Will it allocate memory for all the char's together or allocate for first char. then for int then for float and after this only...
13
by: jm.suresh | last post by:
Hi, I have a program which literately finds the object that overlapping a point. The horizontal and vertical search are called recursively from inside each other. Is this way of implementation...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.