Perl | Subroutines or Functions | Set – 2
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
# Perl program to demonstrate the # passing of hash to subroutines #!/usr/bin/perl # Subroutine definition sub Display_hash { # hash variable to store # the passed arguments my ( %hash_var ) = @_ ; # to display the passed list elements foreach my $key ( keys %hash_var ) { my $val = $hash_var { $key }; print "$key : $val\n" ; } } # defining hash %hash_para = ( 'Subject' => 'Perl' , 'Marks' => 97); # calling Subroutine with hash parameter Display_hash( %hash_para ); |
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
# Perl program to demonstrate the # passing of lists to subroutines #!/usr/bin/perl # Subroutine definition sub Display_List { # array variable to store # the passed arguments my @para_list = @_ ; # to print the passed list elements print "Given list is @para_list\n" ; } # passing scalar argument $sc = 100; # passing list @li = (10, 20, 30, 40); # Calling Subroutine with scalar # and list parameter Display_List( $sc , @li ); |
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
# Perl Program to demonstrate the # returning values subroutine #!/usr/bin/perl # subroutine definition sub Sum { # To get total number # of parameters passed. $num = scalar ( @_ ); $s = 0; foreach $i ( @_ ) { $s += $i ; } # returning sum return $s ; } # subroutine calling and storing result $result = Sum(30, 2, 40); # displaying the result print "Sum of the given numbers : $result\n" ; |
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
# Perl program to demonstrate the Local # and Global variables in subroutine #!/usr/bin/perl # A Global variable $str = "GeeksforGeeks" ; # subroutine definition sub Geeks { # Private variable by using my # keyword for Geeks function my $str ; $str = "GFG" ; print "Inside the Subroutine: $str\n" ; } # Calling Subroutine Geeks(); print "Outside the Subroutine: $str\n" ; |
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
# Perl program to demonstrate the variable # number of parameters to the subroutine #!/usr/bin/perl use strict; use warnings; # defining subroutine sub Multiplication { # private variable containing # default value as 1 my $mul = 1; foreach my $val ( @_ ) { $mul *= $val ; } return $mul ; } # Calling subroutine with 4 parameters print Multiplication(8, 2, 3, 4); print "\n" ; # Calling subroutine again but # with 3 parameters print Multiplication(3, 5, 4); |
192 60
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.