Subroutines are just like functions in different programming languages. In Perl programming we have already used some built-in functions like print, chomp, chop etc User can write own subroutines in Perl. These subroutines can be written anywhere in the program; it's preferable to put the subroutines either at the start or at the tip of the code. Generally, Perl can be defined as shown in below snippet.
[code]
sub subroutine_name{
body of the subroutine
}
[/code]
User need to access to access or call a subroutine by using the name prefixed with '&' symbol is as shown in below snippet.
[code]
sub display
{
print "this is a subroutine";
}
&display(); # This is how we call a subroutine
[/code]
The code below demonstrates the simple functions and calling as shown below.
[perl]
# Function definition
sub Hello{
print "Hello, World!\n";
}
# Function call
Hello();
[/perl]
Result
By running the above code in a Perl command line user can get the following output as shown in below image.
Passing Arguments
The user may pass varied arguments to a subroutine just similar to other programming languages and which can be accessed within the function by using the special array @_. therefore the primary argument to the function is in $_[0], the second is in $_[1], and so on. The user can pass arrays and hashes as arguments like any scalar but passing more than one array or hash normally causes them to lose their separate identities. The below example demonstrates the list of numbers and which can print an average of those numbers.
[perl]
# Function definition
sub Average{
# get total number of arguments passed.
$n = scalar(@_);
$sum = 0;
foreach $item (@_){
$sum += $item;
}
$average = $sum / $n;
print "Average for the given numbers : $average\n";
}
# Function call
Average(10, 20, 30);
[/perl]
Result
By running the above code in a Perl command line user can get the following output as shown in below image.
Perl Shift
User can also use the shift keyword to shift one parameter at a time to a variable or $_[0],$_[1]…. and it is an individual element of @_array. The code below demonstrates the passing hashes to subroutines as shown below.
[perl]
# Function definition
sub PrintHash{
my (%hash) = @_;
foreach my $key ( keys %hash ){
my $value = $hash{$key};
print "$key : $value\n";
}
}
%hash = ('name' => 'antony', 'age' => 19);
# Function call with hash parameter
PrintHash(%hash);
[/perl]
Result
By running the above code in a Perl command line user can get the following output as shown in below image.
Returning Value
Subroutines Main functionality is to do some task and return the result of reusable code which is shown in the below image.
The code below demonstrates the Returning the Values as shown below.
[perl]
# Function definition
sub Average{
# get total number of arguments passed.
$n = scalar(@_);
$sum = 0;
foreach $item (@_){
$sum += $item;
}
$average = $sum / $n;
return $average;
}
# Function call
$num = Average(10, 20, 30);
print "Average for the given numbers : $num\n";
[/perl]
Result
By running the above code in a Perl command line user can get the following output as shown in below image.