Examples for Chapter 3 written in Perl 5 (#71)

* Create 01_countdown.pl

Recursion example #1 written in Perl 5

* Create 02_greet.pl

Recirsion example #2 written in Perl 5

* Create 03_factorial.pl

Recursion example #3 written in Perl 5
This commit is contained in:
biosta
2018-04-29 18:27:14 +03:00
committed by Aditya Bhargava
parent 9744af50ed
commit 67291c451f
3 changed files with 51 additions and 0 deletions

View File

@@ -0,0 +1,15 @@
sub countdown {
my $i = shift;
print "$i\n";
# base case:
return if $i <= 1;
# recursive case
countdown( $i - 1 );
}
countdown(5);

View File

@@ -0,0 +1,23 @@
sub greet2 {
my $name = shift;
print "how are you, $name ?\n";
}
sub bye { print "ok bye !\n"; }
sub greet {
my $name = shift;
print "hello, $name !\n";
greet2($name);
print "getting ready to say bye...\n";
bye();
}
greet('adit');

View File

@@ -0,0 +1,13 @@
sub fact {
my $x = shift;
if ( $x == 1 ) {
return 1;
}
else {
return $x * fact( $x - 1 );
}
}
print fact(5), "\n";