Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: factorial through recursion

by imrags (Monk)
on Jul 24, 2009 at 06:56 UTC ( #782849=note: print w/ replies, xml ) Need Help??


in reply to factorial through recursion

Here's something that might work:

#factorial through recursion use strict; use warnings; print "enter number\n"; my $num=<>; my $fac = &fact($num,1); print "factorial: $fac\n"; ################## sub fact { my($num1,$flag)=@_; if($num1==0) { return $flag; # print "$flag" and die "brrrrrrrreeeeeee :D\n"; } else {fact($num1-1,$num1*$flag);} }
It's good (better/best) to use return statement in recursive code!
Also, instead of shift, use @_
Raghu


Comment on Re: factorial through recursion
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://782849]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (15)
As of 2015-07-01 17:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (15 votes), past polls