Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 5th, 2006, 8:40 PM   #1
baldy1324
Newbie
 
Join Date: Oct 2006
Location: texas
Posts: 3
Rep Power: 0 baldy1324 is on a distinguished road
perl error with recursion

hi i have a program that i tweaked from python (which i know) to perl (changed syntax). here is the program.
#!/usr/bin/perl

sub fib($n)
{
        if ($n<2)
        {
                return 1;
        }
        else
        {
                return fib($n-1) + fib($n-2);
        }
}

$n = 1;
while ($n < 42)
{
        print fib($n);
        print " ";
        $n = $n + 1;
}

when i run the program i get the following.
Not enough arguments for main::fib at fibrecursion.pl line 18, near "$n)"
Execution of fibrecursion.pl aborted due to compilation errors.
any help would be appreciated
thanks
baldy1324 is offline   Reply With Quote
Old Nov 6th, 2006, 3:08 AM   #2
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4 Arevos is on a distinguished road
In perl, you can't pass arguments via the subroutine definition. Instead, you pull the subroutine's arguments from the list @_. Try this instead:
perl Syntax (Toggle Plain Text)
  1. sub fib
  2. {
  3. ($n) = @_;
  4. ...
  5. }
Arevos is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Learning Perl ReggaetonKing Perl 42 Oct 20th, 2006 4:42 PM
Debug recursion method() pr0gm3r Java 3 Oct 11th, 2005 12:33 PM
Recursion Mjordan2nd Coder's Corner Lounge 22 Jul 7th, 2005 11:26 AM
Why do most shared hosting services offer php not perl? Jonnno Perl 5 May 19th, 2005 3:56 PM
Learning and Programming Perl G.I.Josh Perl 2 Mar 23rd, 2005 1:48 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:21 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC