Perl Subroutines

A subroutine is declared with sub. All of its arguments arrive in the special array @_, and my @values = @_ copies them into a named variable so the body is easier to read.

Perl
use strict;
use warnings;

sub average {
    my @values = @_;
    my $total = 0;
    $total += $_ for @values;
    return $total / @values;
}

print average(82, 91, 76, 95), "\n";
Read the full lesson