By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,898 Members | 1,198 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,898 IT Pros & Developers. It's quick & easy.

Recursive function won't compile

P: n/a
#include <stdio.h>
#include <stdlib.h>

def RecursiveFact(n):
if(n>1):
return n*RecursiveFact(n-1)
else:
return 1

fact = RecursiveFact(31)
print fact

fact = "End of program"
print fact
.......but yet it still gives the right answer. How is this possible?
Apr 2 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Apr 2, 5:23*pm, bc1...@googlemail.com wrote:
#include <stdio.h>
#include <stdlib.h>

def RecursiveFact(n):
* * if(n>1):
* * * * return n*RecursiveFact(n-1)
* * else:
* * * * return 1

fact = RecursiveFact(31)
print fact
The output is 8222838654177922817725562880000000 and is correct. But
the "#include"s tell me you're a bit confused. Have you tried running
"python yourscript.py" (where "yourscript.py" is the filename you
saved the above program to)?

HTH,
Daniel
Apr 2 '08 #2

P: n/a
In article <be**********************************@u36g2000prf. googlegroups.com>,
<bc****@googlemail.comwrote:

(Subject: Recursive function won't compile)
>#include <stdio.h>
#include <stdlib.h>

def RecursiveFact(n):
>......but yet it still gives the right answer. How is this possible?
Possibly because it gives the right answer in a different language than
it fails to compile in?
dave

--
Dave Vandervies dj3vande at eskimo dot com
A violent rewrite could produce something worthwhile; as it is you need enough
experience to not need the book to be able to read it. Well, that makes sense
to me, anyhow. --CBFalconer in comp.lang.c
Apr 2 '08 #3

P: n/a
bc****@googlemail.com schrieb:
#include <stdio.h>
#include <stdlib.h>

def RecursiveFact(n):
if(n>1):
return n*RecursiveFact(n-1)
else:
return 1

fact = RecursiveFact(31)
print fact

fact = "End of program"
print fact
......but yet it still gives the right answer. How is this possible?
Given that you obviously don't use python, but some weird cross-breed
beteween python and C - who are we to judge the semantics of that chimera?

Diez
Apr 2 '08 #4

P: n/a
bc****@googlemail.com wrote:
#include <stdio.h>
#include <stdlib.h>

def RecursiveFact(n):
if(n>1):
return n*RecursiveFact(n-1)
else:
return 1

fact = RecursiveFact(31)
print fact

fact = "End of program"
print fact
......but yet it still gives the right answer. How is this possible?
Why not? What did you expect?

Hint:

The first two lines are comments to Python -- so are ignored.

The last two lines just print a string -- no problem there.

The recursive calculation is standard -- the extra set of parenthesis in
the if don't cause any problem.

So again please: Why are you surprised?

Gary Herron

Apr 2 '08 #5

P: n/a
On Apr 2, 5:00 pm, "Diez B. Roggisch" <de...@nospam.web.dewrote:
bc1...@googlemail.com schrieb:
#include <stdio.h>
#include <stdlib.h>
def RecursiveFact(n):
if(n>1):
return n*RecursiveFact(n-1)
else:
return 1
fact = RecursiveFact(31)
print fact
fact = "End of program"
print fact
......but yet it still gives the right answer. How is this possible?

Given that you obviously don't use python, but some weird cross-breed
beteween python and C - who are we to judge the semantics of that chimera?

Diez
Seems like a bad belated April Fool's day joke to me.

George
Apr 2 '08 #6

P: n/a
"Diez B. Roggisch" <de***@nospam.web.dewrites:
>#include <stdio.h>
#include <stdlib.h>

def RecursiveFact(n):
if(n>1):
return n*RecursiveFact(n-1)
else:
return 1

fact = RecursiveFact(31)
print fact

fact = "End of program"
print fact
......but yet it still gives the right answer. How is this possible?

Given that you obviously don't use python, but some weird cross-breed
beteween python and C - who are we to judge the semantics of that
chimera?
What do you mean? Aren't these:

#include <stdio.h.
#include <stdlib.h>

perfectly valid comments in Python?

Followups redirected.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Apr 3 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.