Transcript Slide7

Functions
Function Overview
• A function definition specifies:
– The function’s name.
– A complete description of its actions.
• Function declaration specifies a function’s name,
but not the code.
• Need not specify:
– The number of parameters.
– The parameters’ types.
– The return value’s type.
• Perl functions can be defined anywhere.
– Except in another function.
Function Definitions
• Function definition syntax:
sub function_name Block
• Example:
sub print_header {
print “\n Program Output\n”;
}
• The function declaration would be:
sub print_header;
Direct Function Call
• Invoke a function directly.
• For example:
– print_header ();
• If definition or declaration occurs before the
call, then
– print_header;
Return Values
• Can be specified in 2 ways.
• Call return
sub two_pi_1 {
return (2 * 3.14);
}
• Do nothing and Perl will return the value of
the last evaluated expression.
sub two_pi_2 {
2 * 3.14;
}
Variable Scope
• A variable’s scope is the range of statements
where it can be used.
• All variables seen so far are global.
• Global variables are not appropriate if used
inside a function only.
– Name conflicts.
Local Variables
• Perl provides two ways to create local
variables.
• my declaration:
sub sub1 {
my $sum = 0;
…}
• The variable has the scope of the function’s
block.
– Tip: put at beginning of function.
Local Variables: my
• Any block can include a my declaration.
• The scope is the smallest enclosing block.
• Example:
$temp = 5;
{
my $temp = 10;
if (…) {
my $temp = @list;
…
}
}
• Never put my in a loop.
Local Variable: local
• Another method is the local declaration.
• Variables defined with local have larger
scope.
• They are visible from:
– The block they are defined in.
– Functions called within the block.
– Any functions called by these functions.
• This is called dynamic scoping.
Parameters
• Parameters are the arguments of a function.
• Perl supports:
– Pass by Value: The values of the parameters are
made available (one-way communication).
– Pass by Reference: The address of the
parameters are made available (two-way
communications).
Passing Parameters in Perl
• In Perl, parameters are passed through an
implicit array variable @_.
• Passed through @ARG if this declaration is
included:
– use English
• When a function is called, the parameter
values are copied into @ARG.
• The values of @ARG are copied back out
when the function terminates.
Passing Parameters (cont.)
• When an array is passed, all its values are
copied into @ARG
• Example:
@list = (1, 3, 5);
fun (6, @list);
=>
@ARG → (6, 1, 3, 5)
• When a hash is passed, it becomes a plain
array.
Pass by Value
• Achieved by copying the parameters in @ARG to
local variables.
• Example:
sub sub1 {
my ($x, $y, $z) = @ARG;
…
}
…
sub1 ($a, $b, 7);
• Values of the parameters are not changed.
Pass by Reference
• Two ways to implement this.
• Use @ARG directly
– Changes to @ARG modifies the values of the actual
parameters.
• Example:
sub interchange {
($ARG[1], $ARG[0]) = ($ARG[0], $ARG[1]);
}
…
interchange ($a, $b);
• Inefficient and prevents proper use of hashes.
Pass by Reference
• Pass the references (i.e. addresses) of the
parameters.
– sub1(\$a, \@b);
• This is the preferred method.
– Much more efficient.
– Proper use of arrays.
Indirect Calls
• A function is called indirectly through a
variable containing a reference to the
function.
• Put (\&) in front of the function’s name to
get its address.
– $ref_fun = \&print_header;
• Functions are called with (&)
– &$ref_fun();
Anonymous Functions
• Perl supports anonymous functions.
– Must be called through references.
• Created by assigning a nameless function
definition to a scalar variable.
• Example:
$ref_header = sub {
print “\n Program Output\n”;
}