this works, thanks.  I thought you had to declare the function before
you called it? That is not right?

#!/usr/bin/perl

use warnings;
use strict;

my $num;

print "enter a number: \n";
chomp($num = <STDIN>);

my $x = fact($num);
print "the factorial is: $x\n";

sub fact {
        my $num = shift @_;
        my $res = 1;
        while ($num > 1) {
                $res *= $num;
                $num--;
        }
        return $res;
}












On Tue, Dec 30, 2008 at 7:20 AM, Mr. Shawn H. Corey
<shawnhco...@magma.ca> wrote:
> On Tue, 2008-12-30 at 07:10 -0600, David Ehresmann wrote:
>> I have a factorial script that calls a sub fact that does the
>> factorial and returns a value.  But I get this error when I execute
>> the script:
>>
>> Use of uninitialized value in numeric gt (>) at fact.pl line 22.
>>
>> Here is the script:
>>
>> #!/usr/bin/perl
>>
>> use warnings;
>> use strict;
>>
>> fact();
>
> This calls the sub with an undef arguement.  Just delete this line.
>
>> my $num;
>>
>> print "enter a number: \n";
>> chomp($num = <STDIN>);
>>
>> my $x = fact($num);
>> print "the factorial is: $x\n";
>>
>> sub fact {
>>         my $num = 0;
>>         $num = shift;
>
> Can be done in one line :  my $num = shift @_;
>
>>         my $res = 1;
>>         while ($num > 1) {
>>                 $res *= $num;
>>                 $num--;
>>         }
>>         return $res;
>> }
> --
> Just my 0.00000002 million dollars worth,
>  Shawn
>
> Believe in the Gods but row away from the rocks.
>  -- ancient Hindu proverb
>
>

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to