Functions or subroutines are program units that exist somewhere in your source code, in another module, or are built into Perl. They are written as self-contained blocks of code so that they can be used repeatedly from anywhere in the program whenever they are needed.
print( )
or chomp( )
are library functions that form part of the Perl language. They provide functionalities that are frequently needed. Rather than letting programmers write their own 'print' or 'chomp' routines, library functions have been packaged with the language to help with the most common programming tasks.
There is nothing that prevents programmers to write their own versions of already existing functions. It is usually preferred to use library functions. First, it saves not only development but also debugging time. Library functions have been used by a wider audience in many circumstances. Any bugs are more likely to have been addressed. Furthermore, library functions are usually highly optimized and perform better than any home-grown function written on the fly.
Any block of code that is frequently repeated within a program may be isolated from the main code block and placed into a separate subroutine. Such subroutines are also called user-defined functions (the programmer is the user of the language). Since they are isolated from the main code, they can reused within the same program when needed.
Some languages require that functions appear in a specific location. Perl allows them to be defined anywhere. For easier code maintenance, you should group all functions together and place them consistently in one particular place. Some people put them at the top before the main code block, some put them at the end of the program.
Perl functions are declared by the keyword sub, followed by a function name, a parameter list in parentheses, and finally a statement block. The keyword then associates the function name with the block of code that follows. This block of code is only executed when the function is called by name.
A function may or may not accept data as input. Any individual data item passed into a function is called a parameter. Many programming languages require function declarations to include a list of parameters in parentheses that declares the type of data that the function is going to receive. In Perl, this is not required but is encouraged to include a parameter list for clarity, even when the function will not receive any values.
If a parameter list is included, the declaration becomes a function prototype. Names of variables included as prototypes in the parameter list are only placeholders, however. The value passed into the function must still be assigned to variables in the function body.
Example:
# function prototype with no parameters sub function_name() { [ statement block ] } # function prototype with parameter list sub function_name($parameter1, $parameter2, $parameter3, ...) { [ statement block ] }
A function can be invoked or called from anywhere in a program or script. In Perl, it is called by prepending its name with an ampersand, &
, followed by a list of parameters the function may need in parentheses. A function call passes control of the program to the function, and the statement block associated with the function is executed. When the last statement in the function has been executed, the function exits and program control returns to the main program. A function also terminates if the interpreter encounters the keyword return
.
#function definitions precede the main program block sub print_function() # function prototype with no parameters { print "Hello, function!\n"; } sub print_parameter($var) # function prototype with one parameter { $var = shift(@_); # assign parameter to variable print "Printing function call no: $var\n"; } # ------------ start main program block ------------ # &print_function(); # function call for ($i = 1; $i <= 10; ++$i) { &print_parameter($i); # function call passing a parameter } &print_function(); # function call
If a function returns a value, the function call can be part of an assignment statement.
#function definitions precede the main program block sub return_max_value($num_1, $num_2) # function prototype with two parameters { ($num_1, $num_2) = @_; if ($num_1 > $num_2) { return $num_1; } else { return $num_2; } } # --------- start main program --------- # $returned = &return_max_value(25, 8); # function call print "$returned\n"; # print returned value
Note: There are more ways to call functions in Perl (see also Functions Calls, Details). The method described above is the most explicit and will always work, whereas other methods are 'shorthand' versions, so to speak.
Functions may or may not accept data as input. A data item passed into the function is called a parameter.
All function parameters are passed to a function as a single flat list of scalars. They come into the function as the default parameter array, @_
.
&myFuntion("Hello", "world") will become @_ = ("Hello", "world"), an array of two elements. @myArray = (1, 2, 3); &myFuntion("Hello", @myArray, "world") will become @_ = ("Hello", 1, 2, 3, "world"), an array of five elements. Hashes, too, become flat arrays: %myHash = (California => "Sacramento", Wisconsin => "Madison"); &myFunction(%myHash) will become @_ = ("California", "Sacramento", "Wisconsin", "Madison"), an array of four elements.
All values are passed by reference, that is, any operation on values passed as parameters will take place on the original value defined before the function call. If you want keep the original value unchanged, you must assign it to a new variable in the function.
sub increment($var) { # $passed gets incremented print "In increment(): \$passed = " . ++$_[0] . "\n"; } sub increment_again($var) { # $passed is assigned to local variable, # which gets incremented instead of $passed $var = $_[0]; print "In increment(): \$var = " . ++$var . "\n" } # -------------- start main ----------------- # $passed = 1; print "\nStart: \$passed = " . $passed . "\n"; &increment($passed) . "\n"; print "Returned from increment(): \$passed = " . $passed . "\n"; &increment_again($passed) . "\n"; print "Returned from increment_again(): \$passed = " . $passed . "\n\n";
If you want to keep an array or hash intact and conflate it into the @_
array, you can pass the array or hash to the function as a reference. This is an advanced topic to read up on when you have more experience with Perl.
A function may or may not return data. Data is returned by ending the function with an explicit return
statement and a list of data. These data constitute the return values of the function.
Just as parameters are passed into a function as a flat array of scalars, so are values returned. However, there is no default array to hold return values. Instead, the return values must be assigned to a variable with the function call.
$returned = &my_function($param, $param); # return value is a scalar @returned = &my_function($param [, ... ]); # return value is an array
A function may or may not include an explicit return statement. If return is stated explicitly, any list of values appearing after the statement will be returned as an array and the function will be exited. If an explicit return is not included, the return value will be the result of the last statement of the function.
#function definitions precede the main program block sub return_max_value($num_1, $num_2) # function prototype with two parameters { ($num_1, $num_2) = @_; if ($num_1 > $num_2) { return $num_1; } else { return $num_2; } } # --------- start main program --------- # $returned = &return_max_value(1, 2); # function call print $returned; # print returned value print "\n";
All variables defined in a function become global variables, that is, they become accessible even outside the function. Global variables are a frequent source of program errors. As a code grows and becomes more complex, it also becomes more difficult to maintain an overview of which variables are accessed when. Global variables should be avoided at all cost. If a variable is defined in a function, it can be made private by preceding it with the keyword my.
my $myVar = 23.5;
This will limit the scope of the variable to the current block of code, for example, a function. The variable will not be accessible from outside the function.
In addition to global and private variables, Perl has variables of dynamic scope, which are preceded by the keyword local.
local %myHash = (California => "Sacramento", Wisconsin => "Madison");
These variables are accessible within the current code block and all functions called within this block.
If your global, local, and private variables have the same name, local variables have precedence over global ones and private variables have precedence over global and local ones within the current block.