A sub routine is a special section of code in a program that doesn't give any input unless coupled with another piece of code.
Think of it like a GPS. It will take you where you wanna go, but not unless you pick it up and turn it on.
The code shown in the llama book is this:
sub total {
my $sum;
foreach (@_) {
$sum += $_;
}
$sum;
}
That will go through whatever list you've given it and add all the numbers up: returning the result in $sum. Theres only three basic things you HAVE to know when using sub routines.
1. & is the symbol for a subroutine, when invoking them you must type
&subroutine
whatever input you want to give the subroutine goes on the end like
&total(1..1000)
or
&total(@lines)
2. A sub routine ALWAYS returns the last part of it's code. So if you want &total to return $sum (like it should) you have to make sure $sum is the last coded line of the subroutine (as shown at the top).
3. You MUST declare the variable you will be using. (also shown above.)
Thats all.
I don't understand this business. Not one word.
ReplyDeleteBUt maybe I'm not trying hard enough.
read the one about sexploits, it's got some universal jokes in it you can get but don't worry about trying hard enough. Just stay pretty and funny :P
ReplyDelete