more work with functions

This commit is contained in:
Dan Buch 2011-01-05 21:58:48 -05:00
parent a0a4ec95a9
commit e4d44963fa

View File

@ -3,8 +3,8 @@ use strict;
use warnings;
sub print_error_message {
my $message = shift;
print STDOUT "ERROR: " . $message . "\n";
my ($message) = @_;
print STDOUT "ERROR: " . $message . "!!!\n";
}
@ -13,5 +13,42 @@ print_error_message("something really horrible happened");
print_error_message("something sort of annoying happened");
sub add_two_numbers {
my ($x, $y) = @_;
my $sum = $x + $y;
return $sum;
}
print add_two_numbers(3, 4) . "\n";
print add_two_numbers(5, 6) . "\n";
print add_two_numbers(7, 8) . "\n";
sub add_two_numbers_and_mult_by_three {
my ($x, $y) = @_;
my $sum = add_two_numbers($x, $y);
my $sum_times_three = $sum * 3;
return $sum_times_three;
}
print add_two_numbers_and_mult_by_three(3, 4) . "\n";
print add_two_numbers_and_mult_by_three(5, 6) . "\n";
print add_two_numbers_and_mult_by_three(7, 8) . "\n";
sub factorial {
my ($num) = @_;
if ($num == 1) {
return 1;
} else {
return $num * factorial($num - 1);
}
}
foreach my $n (1 .. 10) {
print factorial($n) . "\n";
}
1;
__END__