Perl functions.

Perl just like other languages allows us to organize code using functions or subroutines. User-defined functions are created in Perl using the following syntax:
sub function_name
{
   function_body
}
Please notice that this syntax does not allow us to define what kind of arguments the functions receives or what kind of value it returns. All we defining is just the name of a new function. Here is s short example of a subroutine that prints an error message end exits the program:
sub print_error
{
   print "Some error occurred. Exiting the program";
   exit 1;
}

Once a function defined all we need to do to use it in our script is to call by name putting parenthesis after the function name (it's recommended to use them even if our function takes no arguments). For example, to call the error function we just defined we need to write:

print_error();

Return Values

To return a value from function we can use return operator. In the example below the function get_array() sorts an array and returns the sorted array:
sub get_array
{
  @my_array = (23, 13, 43, 56, 72, 1, 10);
  @sorted = sort(@my_array);
  return @sorted;
}
If the keyword return is omitted, Perl functions return the last value evaluated by functions. Thus, the following example does exactly the job as the function get_array returning the same sorted array:
sub get_array_2
{
  @my_array = (23, 13, 43, 56, 72, 1, 10);
  sort(@my_array);
}
since the last value evaluated by this function is the new array returned by function sort(). It's convenient to use return operator when function needs to return a value from a middle of a block.

Local Variables

All variables in a function are, by default, global. As we know it's not recommended to use global variables unless we absolutely need it. To create local variables in a function, we use my() or my:
# file: vars.pl
my $i = 10;
my $j = 5;
print "Outside function (before):\n \$i = $i\n \$j = $j\n";
my_vars();
print "Outside function (after) :\n \$i = $i\n \$j = $j\n";

sub my_vars
{
  my $i = 39; # local variable
  $j = 117;   # global variable
  print "Inside function:\n \$i = $i\n \$j = $j\n";
}

Function Arguments

Although we do not declare arguments a function takes while we defining a function, we still may pass arguments to a Perl function. All arguments are passed into a function through the special array @_. Thus, we can send as many arguments as we want and also function may receive different number of arguments. The following example shows how to treat function arguments:
# file args.pl

sub my_args
{
  my $i;
  print "My arguments:\n";
  for($i=0;$i<=$#_;$i++){
    print "Argument $i: $_[$i]\n";
  }
  print "\n";
}

my $name = "arg one";
my @names = ('This', 'is', 'an', 'array');

my_args($name);
my_args($name, 123);
my_args(123, 345, @names, $name);
Please run this script and pay attention to the arguments of the function. Notice that although calling the function my_args() for the third time we are providing only 4 arguments, the function thinks we passed 7 of them That happens because the function treats the whole array as a set of separate arguments.

If we actually need to pass to a function a scalar variable and an array we can use a trick like this:

sub arr_arg
{
  my ($scalar, @array) = @_;
  print "my arguments:\n";
  print "\$scalar = $scalar\n";
  print "\@array  = @array\n";
}
This function separates the very first scalar argument and puts it into local variable $scalar and treats the rest of the arguments as an array and puts this array into the local array @array. Using this trick, we can separate any number of arguments from the beginning of the argument array $_ and treat the rest as an array.