Perl Function Basics
Perl Function Explained – Part 1
Perl Course
Foreword: In this part of the series, I give you the basics of Perl function.
By: Chrysanthus Date Published: 8 Sep 2015
Introduction
Pre-Knowledge
At the bottom of this tutorial, you have links to series you should have learned before reaching here. This series is part of the volume, Perl Course.
Function Example
Consider the following code:
use strict;
sub myFunc
{
my $var1 = 20;
my $var2 = 30;
my $var3 = $var1 + $var2;
}
my $ret = myFunc();
print $ret;
If you try the code you will have 50 at the output. This is a function that adds the number, 20 and 30. In programming you have a function call and a function called. The function call in this code is the expression, myFunc(). The function called is the one having the addition statements. Parentheses for the function call are optional in Perl. So,
myFunc
would have been OK for the function call.
The @_ Special Variable
Inside a function definition, the @_ array holds arguments sent by the function call. @_ is one of Perl’s special variable. The values in the array are in the order in which they were sent by the function call. The elements (values) of this array are identified by $_[0], $_[1], $_[2], etc. Inside the function called (definition), you can use the following array operators on @_: push, pop, shift, and unshift. The above code can be re-written as follows:
use strict;
sub myFunc
{
my $var1 = $_[0];
my $var2 = $_[1];
my $var3 = $var1 + $var2;
}
my $ret = myFunc(20, 30);
print $ret;
The function call here is “myFunc(20, 30)” and has arguments. Whether or not, a function call has arguments, the parentheses are optional. So the following will equally work fine:
myFunc 20, 30;
However, it is advisable to always have the parentheses.
You can use the elements of the @_ array directly (but be careful not to change the values), as in the following code:
sub myFunc
{
my $var3 = $_[0] + $_[1];
}
In Perl, the value of the last expression (statement) is returned. However, that is not good coding. You should have a return statement in your function definition. The above code is better written as follows:
use strict;
sub myFunc
{
my $var1 = $_[0];
my $var2 = $_[1];
my $var3 = $var1 + $var2;
return $var3;
}
my $ret = myFunc(20, 30);
print $ret;
The return value can be interpreted in scalar, list or void context. The above return statement returns a scalar and should be received by a scalar (received by $ret). The following function returns a list and is received by a list (@arr):
sub myFunc
{
#statements
return ("banana", "pear", "orange");
}
my @arr = myFunc();
If a list is returned and received by a scalar, then it is the last value of the list that will be taken by the scalar. If there is nothing to return (void), then write the return statement without a value and do not receive anything, as the following code shows:
use strict;
sub myFunc
{
my $var1 = 20;
my $var2 = 30;
my $var3 = $var1 + $var2;
print $var3;
return;
}
myFunc();
The function called now has a print function, which does not really replace the return statement. For any function definition (called) to be executed, it must be called by a function call.
A function definition is the function called, which has the statements in the block. A function declaration is the first line of the function definition without any parameter list and without any block, typically followed by a semicolon, as in:
sub myFunc;
The following code is alright:
use strict;
sub myFunc;
sub myFunc
{
my $var1 = $_[0];
my $var2 = $_[1];
my $var3 = $var1 + $var2;
}
my $ret = myFunc 20, 30;
print $ret;
You can have a function declaration followed by a function definition. A function will not work, if it has only the function declaration. Well, in many situations, you need just the function definition without the function declaration. Do not confuse between the function declaration, the function call, and the function definition.
The four Array Operators on @_
You can use the array operators, push, pop, shift, and unshift on @_. You should already know how to use these operators on any array. The use is the same here. So, I illustrate the use, only with the shift() predefined function (operator). The above code can be modified as follows:
use strict;
sub myFunc
{
my $var1 = shift @_;
my $var2 = shift @_;
my $var3 = $var1 + $var2;
print $var3;
return;
}
myFunc(20, 30);
The shift function removes the first element of the array and returns the value, making the second element the new first element. The four operators are the default operators for the @_ array; so inside a function definition, you do not really need to type @_ as argument to the four operators. So, the following code works fine:
sub myFunc
{
my $var1 = shift;
my $var2 = shift;
my $var3 = $var1 + $var2;
print $var3;
return;
}
The indicator for a scalar variable is, $. The indicator for an array variable is, @. The indicator for a hash variable is, %. The indicator for a function variable is, &. A function name is a variable name. You do not use & under every situation. You can use & when calling a function; it is optionally used when calling a function. You cannot use it for function declaration or function definition. Try the following code:
use strict;
sub multi
{
print "seen";
}
&multi();
The code works fine.
A Workaround for Constants
The formula to calculate the circumference (distance round) of a circle is:
C = 2 X pi X radius X
where C is the circumference and pi is a constant being equal to 3.14159.
Perl does not have an official way to make a variable constant. So to have a constant, you need a workaround. You can achieve the constant, pi by the following function:
sub pi ()
{
3.14159;
}
Note the presence and position of the empty parentheses, () ; I will talk about its effect later on. This is a function with only one statement, which is a number, the constant. The ending semicolon of the last statement in a block can be omitted. To use the value of the constant, pi, just call the function without parentheses and arguments, as follows:
pi
Now, the value of pi cannot be changed in the program.
You call other constants in the same way by just typing the function call (name).
The above function definition is better written as:
sub pi () {3.14159}
Try the following code:
use strict;
sub pi () {3.14159}
my $C;
my $radius = 10;
$C = 2 * pi * $radius;
print $C;
I tried it and I had, 62.8318 . If the empty parentheses, () is absent, the function will not work as a constant.
That is it for this part of the series. We stop here and continue in the next part.
Chrys
Related Links
Perl BasicsPerl Data Types
Perl Syntax
Perl References Optimized
Handling Files and Directories in Perl
Perl Function
Perl Package
Perl Object Oriented Programming
Perl Regular Expressions
Perl Operators
Perl Core Number Basics and Testing
Commonly Used Perl Predefined Functions
Line Oriented Operator and Here-doc
Handling Strings in Perl
Using Perl Arrays
Using Perl Hashes
Perl Multi-Dimensional Array
Date and Time in Perl
Perl Scoping
Namespace in Perl
Perl Eval Function
Writing a Perl Command Line Tool
Perl Insecurities and Prevention
Sending Email with Perl
Advanced Course
Miscellaneous Features in Perl
Perl Two-Dimensional Structures
Advanced Perl Regular Expressions
Designing and Using a Perl Module
More Related Links
Perl Mailsend
PurePerl MySQL API
Perl Course - Professional and Advanced
Major in Website Design
Web Development Course
Producing a Pure Perl Library
MySQL Course
NEXT