LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-25-2010, 05:05 PM   #1
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 97
Recursive difficulty in Perl


I tried a very simple recursive function in Perl (the classic factorial function), but I couldn't get it to work:

Code:
#!/usr/bin/perl

sub fact {
   $num = $_[0];
   if($num == 1) {
      return 1; # line 6
   } else {
      return $num * &fact($num - 1);
   }
}
$result = &fact($ARGV[0]);
print "$result\n";
The script outputs whatever is returned on line 6, no matter the value of $ARGV[0]. On the other hand, the same function written in Python works perfectly:

Code:
#!/usr/bin/python
import sys

def fact(num):
   if(num == 1):
      return(1)
   else:
      return(int(num) * fact(int(num) - 1))

result = fact(sys.argv[1])
print(result)
It appears that the Perl function is returning as the final value, only what the final iteration of the function returns. Am I missing something about how Perl handles functions and return values?
 
Old 05-25-2010, 05:16 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Use "my" variables

Hi -

This link might help:

http://www.ualberta.ca/~hquamen/303/recursion.html

A language like C supports passing values by value (which automatically lends itself to recursion) or by reference (which doesn't).

Perl passes by reference, which is why you need to make local copies of the input values.

'Hope that helps .. PSM
 
Old 05-26-2010, 07:26 AM   #3
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by paulsm4 View Post
Perl passes by reference, which is why you need to make local copies of the input values.
Of course, Robhogg is trying to do just that when he assigns to $num. The problem is the word "local": $num is not a local variable. It is a global variable. This problem can be fixed by declaring $num to be a local variable thus, just inside the fact function:
Code:
my $num = $_[0];
An idiom often used, especially if several values are being passed to the function, is this:
Code:
my($fred,$barney,$wilma,$betty)=@_;
Hope this helps.
 
1 members found this post helpful.
Old 05-26-2010, 11:21 AM   #4
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Original Poster
Rep: Reputation: 97
Thanks, guys. I forgot about the quirks with variable scope in Perl. D'Oh!
 
Old 05-27-2010, 01:37 AM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Robhogg View Post
Thanks, guys. I forgot about the quirks with variable scope in Perl. D'Oh!
It's not quirks, it's by design and documented. See, for example, this:

http://en.wikipedia.org/wiki/Scope_%28programming%29
.
 
1 members found this post helpful.
Old 05-27-2010, 01:50 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,996

Rep: Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187Reputation: 3187
Please mark as SOLVED if you have your solution.
 
Old 05-27-2010, 05:29 PM   #7
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Original Poster
Rep: Reputation: 97
Wink

Quote:
Originally Posted by Sergei Steshenko View Post
It's not quirks, it's by design and documented...
I'd stand by my use of the word. Yes, it is by design in Perl (something I'd forgotten), but comparing it to the behaviour of a wide range of other languages (C, C++, Java, Python, bash...) it is a tad quirky.

Last edited by Robhogg; 05-27-2010 at 05:31 PM.
 
Old 05-28-2010, 01:25 AM   #8
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Robhogg View Post
I'd stand by my use of the word. Yes, it is by design in Perl (something I'd forgotten), but comparing it to the behaviour of a wide range of other languages (C, C++, Java, Python, bash...) it is a tad quirky.
No, it isn't. There are two scoping models implemented in Perl: dynamic and lexical - in that chronological order.

If you insist on the "quirky" word, then C++ is "C" with later added quirks.

Last edited by Sergei Steshenko; 05-29-2010 at 04:08 AM.
 
Old 05-28-2010, 12:45 PM   #9
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Original Poster
Rep: Reputation: 97
I'm going to resist the temptation to continue arguing.

Last edited by Robhogg; 05-28-2010 at 02:39 PM. Reason: It was getting too silly.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Got to be a better way. Perl recursive directory listing (not files) PB0711 Programming 8 03-16-2009 05:24 PM
Perl file handler in recursive function enemorales Programming 4 02-02-2009 03:20 PM
having difficulty sorting hash alphabetically by value in PERL Joseph Schiller Programming 11 01-06-2009 11:58 AM
rm -r what is recursive wogga Linux - Software 3 05-28-2004 02:29 PM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:13 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration