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. - #include <iostream>
-
using namespace std;
-
-
long factorial (long x)
-
{
-
if (x > 1)
-
return (x * factorial (x-1)); //calling the function from within the function.
-
else
-
return (1);
-
}
-
-
int main ()
-
{
-
long num;
-
cout << "Enter a number: ";
-
cin >> num;
-
cout << num << "! = " << factorial (num);
-
return 0;
-
}
9 1932
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.
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
Banfa 9,065
Recognized Expert Moderator Expert
Looks like it should work to me, what makes you think it wont.
It looks like it is missing a static
You mean static keyword for the function?
Regards
Dheeraj Joshi
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?
It compiles and run's properly. No need of static, for variable x i think.
Regards
Dheeraj Joshi
code green 1,726
Recognized Expert Top Contributor
Apologies. I am seeing brackets that are not even there.
donbock 2,426
Recognized Expert Top Contributor
Use of static or global variables inside a recursive function is typically ill-advised.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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...
|
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,...
|
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...
| |
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...
|
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:...
|
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 =...
|
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?
...
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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: 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: 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...
| |
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...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |