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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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);

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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);

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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";

chevron_right


Output:

Sum of the given numbers : 72


Local and Global Variables in Subroutines: All the variables inside a Perl program are Global vairbales 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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";

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 containg
    # 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);

chevron_right


Output:

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.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.