Tinkering with Perl

(Search & Sitemap) > Writing > Miscellaneous Nonfiction > Tinkering with Perl
Skip Back  Previous  41  42  43  44  45  46  47  48  49  50  Next  Skip Forward
Printer-Friendly Version

Subroutines

A subroutine is a miniature program that you can run from within your program. Earlier, we talked about a procedure that would tell a price for an amount of fruit. Here is how we write such a procedure. Before the procedure is used, we assume that there is some code that tells the price of the different kinds of fruit:

$PricePerApple = .25;
$PricePerOrange = .30;
$PricePerBanana = .20;

And here is the subroutine itself:

sub TellPrice()
    {
    $NumberOfApples = shift(@_);
    $NumberOfOranges = shift(@_);
    $NumberOfBananas = shift(@_);
    @RemainingArguments = @_;
    $PriceForApples = $NumberOfApples * $PricePerApple;
    $PriceForOranges = $NumberOfOranges * $PricePerOrange;
    $PriceForBananas = $NumberOfBananas * $PricePerBanana;
    $TotalPrice = $PriceForApples + $PriceForOranges + $PriceForBananas;
    print "The total price for the fruit is $TotalPrice.\n";
    }

Well, let's look at it piece by piece:

    $NumberOfApples = shift(@_);
    $NumberOfOranges = shift(@_);
    $NumberOfBananas = shift(@_);
    @RemainingArguments = @_;

This part finds out what the arguments to the procedure are, and stores them in the appropriate variables. This should go at the beginning of the subroutine; the syntax for using the subroutine is:

&TellPrice($NumberOfApples, $NumberOfOranges, $NumberOfBananas);

The part of the subroutine that gets the arguments should be of the following form:

    $first argument = shift(@_);
    $second argument = shift(@_);
    $third argument = shift(@_);
    ...
    @RemainingArguments = @_;

where first argument, second argument, and so on are the names of the arguments in order.

After firuging out what the arguments are, the subroutine then does some arithmetic to figure out the total price, and prints it out.

One more thing... At the beginning of the program, you need to declare your subroutine, by having a line like this:

sub &TellPrice();

See also:

Statements - Flow control - Blocks - Subroutines and functions - Arguments - Functions

Tinkering with Perl is a free book that provides an introduction to programming in Perl, as well as a basic reference for things like foreach in Perl, if-then, and if-then-else, in addition to providing a glossary where you can find definitions for concatenate and other terms.

Tinkering with Perl may be one of the most popular offerings on this site, but it's not the only attraction. You can read a tongue-in-cheek Game Review: Meatspace, read an even more offbeat customer service survey (whether or not you actually fill it out), and spend a few minutes wishing your boss would read, The Administrator Who Cried, "Important!" (Not to mention that there are other things you can read here besides tech stuff, from Janra Ball: The Headache to The Spectacles.)

Read more...

Top

(Search & Sitemap) > Writing > Miscellaneous Nonfiction > Tinkering with Perl
Skip Back  Previous  41  42  43  44  45  46  47  48  49  50  Next  Skip Forward
Printer-Friendly Version