3. Functions

Functions are a fundamental part of most programming languages. They often behave like an operator, producing a change in the value of some variable or returning a value that can be assigned to a variable. They also control the flow of execution, transferring control from the point of invocation to the function definition block and back. Thus, they combine properties of the two preceding discussions.

The discussion will cover both the designation of functions and their invocation and use.


invocation

The function is invoked within the context of some expression. It is recognized in context by the form of its name: an ampersand is placed before the name when the function is called. If the function takes arguments, they are placed within parentheses following the name of the function.

Form:

&name()

Example:

&aFunction()
definition

The function definition is marked by the keyword, sub; followed by the name of the function, without the ampersand prefix. It is followed by the block of code that is executed when the function is called, enclosed within curly braces.

Example:

sub aFunction { stmt_1; stmt_2; stmt_3; }
To use functions effectively, we need three additional concepts: return values, arguments, and local variables.
return values

The value returned by a Perl function is the value of the last expression evaluated in the function.

Example:

sub aFunction { stmt_1; stmt_2; $a = $b + $c; }

In this example, the function will return the value of $a at the time when the function ends. Note: operators, such as print return values of 0 or 1, indicating failure or success. Thus, print ($a); as the last statement in a function would result in a return of 0 or 1 for the function, not the value of $a.

arguments

Arguments are enclosed in parenthses following the name of the function during invocation; thus, they constitute a list. They are available within the function definition block through the predefined (list) variable, @_.

Example:

&aFunction ($a, "Literal_string", $b); sub aFunction { foreach $temp(@_) { print "$temp \n"; } }

local variables

Any variables defined within the body of a Perl program are available inside a Perl function as global variables. Consequently, Perl provides an explicit local operator that can be used to limit the scope of variables. Thus, one can define variables that are local to a function so that their use will not produce inadvertent side effects with any global variables that may have the same names. By the same token, they will not be visible outside of the function.

Local variables are, by convention, defined at the top of a Perl function. They are defined by the keyword, local, followed by a list of variable names, within parentheses.

Example:

&aFunction ($a, $b); sub aFunction { local ($aLocal, $bLocal); $aLocal = $_[0]; $bLocal = $_[1]; }

$aLocal and $bLocal will have the same values inside the function as $a and $b have at the time the function was invoked. Changes to either local variable inside the function, however, will not affect the values of $a or $b.