474,042 Members | 1,837 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

stack operation in recursive function

40 New Member
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 1673
Ganon11
3,652 Recognized Expert Specialist
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
6748
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, Allen
4
9077
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. However, it is not as straightforward to determine the base address for my stack space. The approach I have taken is to save the address of an automatic variable in main( ), and assume this is a fairly good indicator of my base address. Then, I can...
5
502
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) { do required stuff if(some conditions obeyed)
13
2085
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
25626
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 possible to determine the amount of memory used in the current stack? Any assistance would be appreciated. Ben
4
3872
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); And the total code is static void Main(string args)
2
2238
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 beginning of each rule/function, I make a call to inspect.stack() (I think...) and that fetches the name from someplace. However, with a bunch of test text input, this takes ~10 seconds to run. When I comment out the inspect.stack() function from each...
13
2142
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 second char in the function gets memory. 2) If i declare variables where the size of al variables gone beyond sizeof stack (assume it is 1000) what 'll happen? voud foo( int a, innt b)
13
2104
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 fill the stack space with the local variables inside each call. If this is not good, is there a better way to implement? Or python itself will understand that the calls happen in the last line, so local variables need not be pushed into the stack?
0
10539
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10337
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
12012
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
11138
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8693
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7865
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6831
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
5408
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3967
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.