Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-24 22:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found