Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Perl (http://www.programmingforums.org/forum21.html)
-   -   perl error with recursion (http://www.programmingforums.org/showthread.php?t=11813)

baldy1324 Nov 5th, 2006 8:40 PM

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

Arevos Nov 6th, 2006 3:08 AM

In perl, you can't pass arguments via the subroutine definition. Instead, you pull the subroutine's arguments from the list @_. Try this instead:
:

  1. sub fib
  2. {
  3.         ($n) = @_;
  4.         ...
  5. }



All times are GMT -5. The time now is 9:39 PM.

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