Subroutines and Functions
This document was written by CS 290W TA David Corcoran and was last
modified
Like other programming languages Perl allows you to reuse code by
implementing your code into a subroutine. A subroutine is the same
thing as a function except in Perl you do not have to say what
parameter values that you pass to that function. To create and use a
subroutine follow the following example:
# Declaring a subroutine
sub fWelcome
{
print "Welcome Back\n";
}
&fWelcome;
Subroutines are declared by using the sub keyword and placing
all executed statements for that routine within brackets like in other
languages. Although Perl executes its code from top to bottom, the
subroutine will NOT be executed until it is invoked. Notice the
statement
&fWelcome;
This is telling Perl that you would like to execute a subroutine
called fWelcome. By the way, although it is fairly common to use the
& in this manner to say "call this function for me", the & is only
REQUIRED if the subroutine is defined after the call to it. Since
that is not the case above
fWelcome;
would also work!
Let's say we wanted to have a function that returned a value such as
age. We might do something like the following:
# Declaring a subroutine with return value
sub fWelcome
{
$iAge = 13;
print "Welcome Back\n";
$iAge;
# Perl returns the last variable.
}
$iHowOld = &fWelcome;
# $iHowOld gets value of $iAge.
Here we are just saying $iAge but not doing anything to it. Actually
Perl will use this last variable as a return value for the function.
You could also have an array like @aiAge instead of the scalar
$iAge.
You can also use a return statement to exit a function and
return a value.
sub fAverage
{
if ($iStudents != 0)
{
return ($iTotalScore / $iStudents);
}
else
{
return (-99);
}
}
Now you might ask "How does one pass arguments to a Perl Function?"
Well, here is when it gets kind of tricky.
# Declaring a subroutine with passed
# arguments and return value
sub fWelcome
{
my ($iLocal);
# Local variable.
$iAge = $_[0];
# First parameter
$iWeight = $_[1];
# Second parameter
($iAge,$iWeight) = @_;
# Same as above.
$iLocal = $iAge + $iWeight;
# Add the two.
$iBoth = $iLocal;
print "Welcome Back\n";
$iBoth;
# Perl returns the last variable.
}
$iResult = &fWelcome(9, 75);
# 9 years old, 75 lbs.
Here arguments are passed to the function in a usual way. But notice
those ugly $_[0] array subscript variables. The variable $_ is the
Perl default. Most Perl operations will default to this variable if
you do not specify one. Perl also uses this to pass arguments to a
function and they are passed in as an array as noted above. Also
notice that my() statement. This is telling Perl to make $iLocal a
local variable to that function. This means that outside function
fWelcome, $iLocal is unavailable. (Variables declared with my() are
totally hidden from the outside world, including any called
subroutines.) Even though $iBoth is declared in that function, it is
still global and will be available outside function fWelcome.
sub fAdd
{
$iAge = $_[0];
$iNum = $_[1];
$iDat = $_[2];
$iSum = $iAge + $iNum + $iDat;
}
@aiBunch = (14, 25, 31);
$iWhatever = &fAdd(@aiBunch);
# Whatever is 70
Notice above that an array can be passed to a subroutine by just
giving its name as the parameter.
sub fAdd
{
$iAge = $_[0];
$iNum = $_[1];
$iDat = $_[2];
$iSum = $iAge + $iNum + $iDat;
}
@aiBunch = (14, 25);
$iLast = 31;
$iWhatever = &fAdd(@aiBunch, $iLast);
# Whatever is 70
Notice above that an array can be passed along with scalar values to a
subroutine.
Subroutine fAdd can also be re-written:
sub fAdd
{
@aiAge = @_;
$iSum = $aiAge[0] + $aiAge[1] + $aiAge[2];
}