perlguru22 has asked for the wisdom of the Perl Monks concerning the following question:
1. Write a subroutine (&evens) that recieves an array of numbers and returns an array containing only even numbers Give an example of calling the subroutine.
#!/usr/bin/perl use strict; use warnings; my@num = (1..10); sub even { foreach my$number(@num) { if ($number%2==0) { my @evens = $number; print "@evens"; } } } &even
2. Write a subroutine(&squares) that recieves an array of numbers and squares each number in the array. Note: nothing is returned.
#! /usr/bin/perl use strict; use warnings; my @nums = (1..10); sub square { foreach my$number (@nums) { my$number = ($number * $number); print "$number\n"; } }
3. Write a subroutine(&huge) that returns true if the number sent is greater than 1,000,000 or false(look up what is true and false). Show an example of calling the function.
#!/usr/bin/perl use strict; use warnings; sub huge { my $num = <>; if ($num > 1000000) { print "1"; } else{ print "0"; } } &huge
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Assignments for Subroutines
by tobyink (Canon) on Sep 27, 2012 at 07:52 UTC | |
|
Re: Assignments for Subroutines
by Athanasius (Archbishop) on Sep 27, 2012 at 07:46 UTC | |
|
Re: Assignments for Subroutines
by Anonymous Monk on Sep 27, 2012 at 07:30 UTC | |
|
Re: Assignments for Subroutines
by protist (Monk) on Sep 27, 2012 at 10:00 UTC | |
by nemesdani (Friar) on Sep 27, 2012 at 10:29 UTC | |
by tobyink (Canon) on Sep 27, 2012 at 16:17 UTC | |
by protist (Monk) on Sep 27, 2012 at 10:55 UTC | |
|
Re: Assignments for Subroutines
by aaron_baugher (Curate) on Sep 27, 2012 at 13:56 UTC |