![]() |
|
|
|
| ||||||
|
Welcome to the The ProgrammersTalk Community forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
| Tags: |
![]() |
![]() | | LinkBack | Thread Tools | Display Modes | ![]() |
| |||
| [SOLVED] Does the following PERL program pass a list/array of numbers to the add an.. ...multiply subroutine as a referecenc Does the following PERL program pass a list/array of numbers to the add and multiply subroutine as a referecence or does it not? And if doesn't how can make it do this. my ($rtn, $len); $len = @ARGV; # If first argument is add, run add function; if multiply, run multiply # function, otherwise warn user and end script if ($ARGV[0] =~ /add/) { $rtn = &add(@ARGV); print "The sum is: $rtn\n"; } elsif ($ARGV[0] =~ /multiply/) { $rtn = &multiply(@ARGV); print "The product is: $rtn\n"; } else { print "\nInvalid request, please use \"add\" or \"multiply\"\n"; print "followed by some numbers.\n"; } # Subroutine definitions # sub add { my ($sum, $i); $sum = 0; for ($i = 1; $i <= $len; $i++) { $sum += $_[$i]; } $sum; } sub multiply { my ($product, $i); $product = 1; for ($i = 1; $i < $len; $i++) { $product *= $_[$i]; } $product; } |
| |
![]() |
| Thread Tools | |
| Display Modes | |
| |