Perl Programming Page 4 - Perl 101 (Part 5) - Sub-Zero Code |
Usually, when a subroutine is invoked in Perl, it generates a "return value". This return value is either the value of the last expression evaluated within the subroutine, or a value explicitly returned via the "return" statement. We'll examine both these a little further down - but first, here's a quick example of how a return value works. In this case, the value of the last expression evaluated within the subroutine serves as its return value - this value is then assigned to the variable $result when the subroutine is invoked from within the program. Of course, it's also possible to explicitly specify a return value - use the "return" statement, as we've done in the next example:
This article copyright Melonfire 2000. All rights reserved.{mospagebreak title=Jumping Cows And Extra-Large Pumpkins} Return values from a subroutine can even be substituted for variables anywhere in a program. For example, you could modify the last two lines of the example above to read: And, of course, return values need not be scalar variables alone - a subroutine can just as easily return an array variable, as we've demonstrated in the following example: The output is Word: The Word: cow Word: jumped Word: over Word: the Word: moon Word: and Word: turned Word: into Word: a Word: gigantic Word: pumpkin The number of words in the given string is 12 This article copyright Melonfire 2000. All rights reserved.
blog comments powered by Disqus |