This is my first experience with subroutines ever, and I'm straight up confused.

Ok, so you were sick the day they covered subroutines, I'll try to clear them up a bit. You were given a subroutine with some commonly used shortcuts. Here's a different example broken down into four versions that do exactly the same thing.

# calling the subroutine. in this example the # first arg is a variable, second is a literal value my $arg1 = 100; my $return_value = add_these($arg1, 200); print $return_value; # subroutines receive arguments via an array named @_ # this version gets the args by array index sub add_these{ my $first = $_[0]; my $second = $_[1]; my $answer = $first + $second; return $answer; } # this one shifts values off the front of the array # note that @_ is implied if an array is not given as # the argument to shift. commonly seen as: # my $value = shift; sub add_these{ my $first = shift @_; my $second = shift @_; my $answer = $first + $second; return $answer; } # as shown here it is not necessary to assign the # arguments to an intermediate variable - you can # use them directly (however the extra variables # often makes the code more readable) sub add_these{ my $answer = $_[0] + $_[1]; return $answer; } # you don't even need to return a variable, you can # directly return the value calculated by an expression, # as shown here and in the "hint" example you were given sub add_these{ return $_[0] + $_[1]; }

As for the multiples problem, you're looking for a boolean (true/false) value, represented as true = 1, false = 0. So your subroutine would determine if the arguments were multiples and return 1 if they are, 0 if they're not. And yes, use an if statement, (and the modulo operator as others suggested). There are other ways to do it, but no sense getting fancy till you have a handle on the basics.


In reply to Re: finding multiples of two values by hangon
in thread finding multiples of two values by urnumdei

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.