You are close. All arguments to a subroutine are passed as a single ordered array. That array is accessed in the subroutine by @_.
 my ($average_num, @nums) = @_; means take the first element of @_ and make a copy of it into the subroutine's variable $average_num, then copy all of the elements that are "left over" to the subroutine's array of @nums.

my ($average_num, $first_num, @rest_of_nums) = @_; is possible. But you can't have my ($average_num, @nums, $last_num) = @_; because @nums will consume all entries that are left in @_ and $last_num will be undefined.

The @_ array is very special and some tricks can be played in certain circumstances. None of which apply in 99.9% of Perl code.

Consider the following:

use strict; use warnings; my @numbers = qw(4 12 18 21 35); my $average = find_average(\@numbers); print "The average found for this list is: $average \n" ; ## find_average will cause a fatal error if ## there are no numbers in the array. sub find_average { # subroutine gets a sinle value which is a reference # to an array, not an arry itself. This is much, much # faster if say the array has say 1,000 elements. my $num_array_ref = shift; # could be: my ($num_array_ref) = @_; # The shift operation is very slightly faster if # only one value is involved. my $sum; # no need to set $sum=0; # but I wouldn't quibble if you did that. foreach my $num (@$num_array_ref) { $sum += $num; } my $avg = $sum / @$num_array_ref; return $avg; } __END__ The average found for this list is: 18
Update: I know that some Monks would say that the "return $avg" statement is extraneous because by default, Perl will return the value of the last statement in the subroutine. I don't want to relitigate that except to say that an explicit return statement is in my opinion good practice.

In reply to Re^3: Passing Variables by Marshall
in thread Passing Variables by catfish1116

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.