Prerequisite: Subroutines or Functions in Perl
A Perl function or subroutine is a group of statements that together perform a specific task. In every programming language user want to reuse the code. So the user puts the section of code in function or subroutine so that there will be no need to write code again and again. In this article we will discuss the following concepts:
- Passing Hashes to Subroutines
- Passing Lists to Subroutines
- Returning Value from a Subroutine
- Local and Global Variables in Subroutines
- Different number of parameters in subroutine call
Passing Hashes to Subroutines: A hash can also be passed to subroutines which automatically converted into its key-value pair.
Example:
Perl
sub Display_hash {
my ( %hash_var ) = @_ ;
foreach my $key ( keys %hash_var )
{
my $val = $hash_var { $key };
print "$key : $val\n" ;
}
}
%hash_para = ( 'Subject' => 'Perl' , 'Marks' => 97);
Display_hash( %hash_para );
|
Output:
Marks : 97
Subject : Perl
Passing Lists to Subroutines: As we know that @_ is a special array variable inside a function or subroutine, so it is used to pass the lists to the subroutine. Perl has a different way to accept and parse arrays and lists that make it difficult to extract the discrete element from @_. In order to pass a list along with other scalar arguments, it is necessary to make the list as the last argument.
Example:
Perl
sub Display_List {
my @para_list = @_ ;
print "Given list is @para_list\n" ;
}
$sc = 100;
@li = (10, 20, 30, 40);
Display_List( $sc , @li );
|
Output:
Given list is 100 10 20 30 40
Returning Value from a Subroutine: A subroutine can also return a value like in other programming languages such as C, C++, Java etc. If the user will not return a value from subroutine manually, then the subroutine will return a value automatically. In this, the automatically returned value will be the last calculation executed in the subroutine. The return value may be scalar, array or a hash.
Example:
Perl
sub Sum {
$num = scalar ( @_ );
$s = 0;
foreach $i ( @_ )
{
$s += $i ;
}
return $s ;
}
$result = Sum(30, 2, 40);
print "Sum of the given numbers : $result\n" ;
|
Output:
Sum of the given numbers : 72
Local and Global Variables in Subroutines: All the variables inside a Perl program are Global variables by default. But with the help of my keyword, you can create the local or private variables inside a block. A private variable has a limited scope like between the block(if, while, for, foreach etc.) and methods etc. Outside block or method, these variables can’t be used.
Example:
Perl
$str = "GeeksforGeeks" ;
sub Geeks {
my $str ;
$str = "GFG" ;
print "Inside the Subroutine: $str\n" ;
}
Geeks();
print "Outside the Subroutine: $str\n" ;
|
Output:
Inside the Subroutine: GFG
Outside the Subroutine: GeeksforGeeks
Different number of parameters in subroutine call: Perl does not provide us any built-in facilities to declare the parameters of a subroutine, which makes it very easy to pass any number of parameters to a function.
Example:
Perl
use strict;
use warnings;
sub Multiplication {
my $mul = 1;
foreach my $val ( @_ )
{
$mul *= $val ;
}
return $mul ;
}
print Multiplication(8, 2, 3, 4);
print "\n" ;
print Multiplication(3, 5, 4);
|
Note: Generally, passing more than one array or hash as parameters to subroutines causes them to lose their separate identities. Similarly, returning more than one array or hash from subroutine also causes to lose their separate identities. We can solve these problems by using references.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!