Call the subroutine with $number : Subroutine Call « Subroutine « Perl






Call the subroutine with $number

   

#!/usr/bin/perl

$number=<>;    # read a number from the keyboard
chomp $number; # remove linefeed


$factorial=factorial($number);

# The subroutine
sub factorial {
    $input = shift; # read passed argument
    return 0 if $input==0;
    $result=1;
    foreach (1 .. $input) { # '..' generates a range
        $result *= $_;
    }
    return $result;
}

print "$number factorial is $factorial\n";

   
    
    
  








Related examples in the same category

1.Call a subroutine and use it
2.Call a subroutine without defining it
3.Call sub
4.Calling a sub routine with &
5.Calling a sub routine with 'subroutine_name();'
6.Calling function in print statement
7.Calling subroutines that are not defined before use