Subroutines are sections of code that can take arguments, perform some operations with them, and may return a meaningful value.
Subroutines help our programming for two main reasons.
Output: This is Subroutine.
2) Passing an argument to Subroutine. Parameters are accessed inside a subroutine using special array @_ i.e it holds all arguments passed. Hence, the arguments will start with $_[0], $_[1], $_[2], $_[3] and so on.
Output: Sum of the given numbers : 15
Example of accessing the parameters in subroutine
Single Parameter:
$x = shift; #Shift operator removes and return the first value
$x = $_[0]; #first argument to the function is in $_[0]
Multiple parameters:
$x = $-[1]; # the second parameter is in $_[1]
my ($p1, $p2) = @_; #if there are two arguments
foreach $item (@_) # For each loop to access all elements
my @list = @_ # Collecting all elements in list
my (%hash) = @_; # Collecting all elements in hash
3) Return value by subroutine
Output: 15
4) Passing arrays and hashes to Subroutine.
Subroutines help our programming for two main reasons.
- First, They let us reuse code, This makes it easier for us to write programs.
- The second reason is that they allow us to chunk our code into organizational sections.
1 2 3 4 5 6 | # Function definition sub demo{ print "This is Subroutine.\n"; } # Function Call demo; |
2) Passing an argument to Subroutine. Parameters are accessed inside a subroutine using special array @_ i.e it holds all arguments passed. Hence, the arguments will start with $_[0], $_[1], $_[2], $_[3] and so on.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | # Function definition sub addition{ # get total number of arguments passed. $num = scalar(@_); $sum = 0; foreach $item (@_) { $sum = $sum + $item; } print "Sum of the given numbers : $sum\n"; } # Function Call addition(5,10); |
Example of accessing the parameters in subroutine
Single Parameter:
$x = shift; #Shift operator removes and return the first value
$x = $_[0]; #first argument to the function is in $_[0]
Multiple parameters:
$x = $-[1]; # the second parameter is in $_[1]
my ($p1, $p2) = @_; #if there are two arguments
foreach $item (@_) # For each loop to access all elements
my @list = @_ # Collecting all elements in list
my (%hash) = @_; # Collecting all elements in hash
3) Return value by subroutine
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | # Function definition sub addition{ # get total number of arguments passed. $num = scalar(@_); $sum = 0; foreach $item (@_) { $sum = $sum + $item; } return $sum; } # Function Call $result = addition(5,10); print $result |
4) Passing arrays and hashes to Subroutine.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | @fruits = ('apple', 'orange', 'banana'); %data = ('Jerry' => 32, 'Adam' => 31, 'Chris' => 22); # Function Call printArrayElements(@fruits); printHashElements(%data); # Function definition sub printArrayElements{ my @list = @_; print "Given list is @list\n"; } sub printHashElements{ my %hash = @_; foreach my $key ( keys %hash ) { my $value = $hash{$key}; print "$key : $value\n"; } } |
This comment has been removed by a blog administrator.
ReplyDelete