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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |