As the other answer said, none of these functions receive any arguments, nor do they return anything meaningful.

In Perl, to pass arguments to a function, you put them in parentheses after the function name, a la:

my @nums = 1 .. 10; my @results = evens(@nums);

And within the functions, to receive arguments, you pull them out of the @_ array.

sub evens { my (@nums) = @_; ...; }

To return something, you either use the return keyword, or make sure that the thing you want to return is the last statement executed in the sub.

sub this_sub_returns_1 { return 1; 2; } sub this_sub_returns_2 { 1; 2; }

Here are even and square rewritten to actually take arguments and return values. There's also even_better and square_better which do the same thing but more concisely. (Whether they are "better" is a judgement call. I prefer them though.)

use strict; use warnings; # Write a subroutine (&evens) that recieves an array of numbers and re +turns # an array containing only even numbers Give an example of calling the + # subroutine. sub even { my (@nums) = @_; my @evens; foreach my $number (@nums) { if ($number % 2 == 0) { push @evens, $number; } } return @evens; } print "Results for even(1..10)\n"; print "$_\n" for even( 1 .. 10 ); sub even_better { grep { $_ % 2 == 0 } @_; } print "Results for even_better(1..10)\n"; print "$_\n" for even_better( 1 .. 10 ); # Write a subroutine(&squares) that recieves an array of numbers and # squares each number in the array. Note: nothing is returned. sub square { foreach my $number (@_) { $number *= $number; } return; } print "Results for square(1..10)\n"; my @nums = 1 .. 10; square(@nums); print "$_\n" for @nums; sub square_better { $_ *= $_ for @_; } print "Results for square_better(1..10)\n"; my @nums_better = 1 .. 10; square_better(@nums_better); print "$_\n" for @nums_better;
perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

In reply to Re: Assignments for Subroutines by tobyink
in thread Assignments for Subroutines by perlguru22

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.