473,503 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Recursivity Explanation in c++

37 New Member
Hi

Say we enter 4 at the prompt, factorial function is called with 4 as the argument. Now 4*factorial(4-1). The function calls itself now. So what happens at this point?

1.Does it return anything to main or does it call the function again from within?

2. Where would it hold the 4, the 3, and the 2 to multiply it to get 24? In other words, what is the step by step procedure to get 24?

Thanks a lot.


Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. long factorial (long x)
  5. {
  6.   if (x > 1)
  7.    return (x * factorial (x-1)); //calling the function from within the function.
  8.   else
  9.    return (1);
  10. }
  11.  
  12. int main ()
  13. {
  14.   long num;
  15.   cout << "Enter a number: ";
  16.   cin >> num;
  17.   cout << num << "! = " << factorial (num);
  18.   return 0;
  19. }
Apr 28 '10 #1
9 1932
newb16
687 Contributor
1. It will return to main after everything is calculated.
2. Most likely on the stack, where local variables and passed parameters are stored*. When it multiplies 2 by 1, there are three stack frames with local variables - for factorial called with argument equal 4,3, and 2.

Yes, they can be also passed in registers.
Apr 28 '10 #2
code green
1,726 Recognized Expert Top Contributor
Logically it would use the mathematical BODMAS rule.
So the command in the bracket would be implemented first.
So factorial would be called again and again until !(x>1).

Nothing could be returned to main until !(x>1).then the else is called.
I don't believe this function works. Does it?

It looks like it is missing a static
Apr 28 '10 #3
Banfa
9,065 Recognized Expert Moderator Expert
Looks like it should work to me, what makes you think it wont.
Apr 28 '10 #4
Dheeraj Joshi
1,123 Recognized Expert Top Contributor
It looks like it is missing a static
You mean static keyword for the function?

Regards
Dheeraj Joshi
Apr 28 '10 #5
code green
1,726 Recognized Expert Top Contributor
It looks like the value of (x-1) is continually passed to factorial()
without even the x * being implemented.

I mean a static variable to hold the value of x.
Or pass x as a pointer or reference.

I am beginning to doubt myself. Am I missing something here?
Apr 28 '10 #6
Dheeraj Joshi
1,123 Recognized Expert Top Contributor
It compiles and run's properly. No need of static, for variable x i think.

Regards
Dheeraj Joshi
Apr 28 '10 #7
code green
1,726 Recognized Expert Top Contributor
Apologies. I am seeing brackets that are not even there.
Apr 28 '10 #8
donbock
2,426 Recognized Expert Top Contributor
Use of static or global variables inside a recursive function is typically ill-advised.
Apr 28 '10 #9
Deanm007
37 New Member
Well, I just got here but it appears someone already chose newb16 as best answer for me. LOL. This is too funny. Anyway, it is the best answer. Thanks to everyone for chipping in.. By the way, it does compile and run.
Apr 28 '10 #10

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

Similar topics

2
4737
by: Swartz | last post by:
Hi all. I'm building a development webserver (redhat-based). I'm trying to compile PHP (v4.3.4 if anyone cares) with all the features I might require in the near future. I've ran into a problem...
0
1215
by: Premshree Pillai | last post by:
Hey, For the uninitiated: an explanation of makeExe.py (http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/266471), in the form of an article, “Create Python Executables Automatically”, is...
3
2504
by: David MacQuigg | last post by:
I am writing a chapter for teaching OOP in Python. This chapter is intended as a brief introduction to replace the more complete discussion in Learning Python, 2nd ed, pp. 295-390. I need to...
2
507
by: MatthewRoberts | last post by:
Howdy All, I have a Windows Service that often stops in its tracks with no exception and no explanation on our QA system. During testing on the development machine, it can handle any workload,...
1
3321
by: jimfortune | last post by:
From: http://groups-beta.google.com/group/comp.databases.ms-access/msg/769e67e3d0f97a90?hl=en& Errata: 19 solar years = 2939.6018 days should be 19 solar years = 6939.6018 days Easter...
2
2412
by: Dave Taylor | last post by:
Is there a decent explanation of how menu merging with MDI forms work in VB.NET? I've read through the online help and it still seems that whenever I change menus around or whatever, it breaks...
12
2086
by: jacob navia | last post by:
Hi I am writing this tutorial stuff again in the holidays and I came across this problem: The "width" field in printf is a minimum width. Printf will not truncate a field. for instance:...
4
1527
by: dismantle | last post by:
Hi all, this is my 3rd week in studying VB codes and i came across with this codes from a online tutorial about classes. Public Function MiddleInitial() As String MiddleInitial =...
6
2459
by: WolfgangS | last post by:
Ok first off, i am a total beginner at this groups stuff and i have no clue how this works. This is probabaly the wrong group for my problem but i will post it anyways. Learning by doing right? ...
16
1636
by: DamienS | last post by:
In the interests of me saving hair, can someone please explain to me what's going on below? Why doesn't == work in comparing two int's when cast as objects? They're the same type. Note that it...
0
7202
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
7084
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
7328
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
7458
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
5578
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,...
1
5013
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...
0
4672
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...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.