While Perl does not provide any built-in facilities to declare the parameters of a subroutine, it makes it very easy to pass any number of parameters to a function.

This makes it almost trivial to write functions such as sum.

#!/usr/bin/perl
use strict;
use warnings;
use 5.010;

say sum(3, 7, 11, 21);

my @values = (1, 2, 3);
say sum(@values);

sub sum {
    my $sum = 0;
    foreach my $v (@_) {
        $sum += $v;
    }
    return $sum;
}

In this example, first we passed four numbers to the sum subroutine, and in the second call we passed an array that has three numbers in it.

The subroutine itself receives the parameters in the standard @_ variable. In this case we do not copy the values to private variables as the functions is really simple. We just iterate over the values using a foreach loop and add each value the $sum variable.

The return call will, pass the value of $sum to the caller.

Private array

We could have copied the content of @_ to a private variable declared within the subroutine, but it was not necessary in this example.

sub sum {
   my @values = @_;
   ...

Passing more than one array

Unfortunately, if you'd like to pass two arrays to a function like in the following example, you are in trouble.

my @good = ('Yoda', 'Luke', 'Leia');
my @evil = ('Darth Vader', 'Emperor');
print award(@good, @evil), "\n";

The subroutine will see all the values ('Yoda', 'Luke', 'Leia', 'Darth Vader', 'Emperor') in the @_ array, and there will be no easy way to tell which value came from the first array, and which from the second. In order to solve this we'll need to learn about references.

There is an article explaining how to pass two arrays to a function available to Perl Maven Pro subscribers.