Updated April 4, 2023
Introduction to Perl function
In Perl, the function is defined as a set of logical code that can be written within the Perl program, which is in need to write the same logical code again and again. Therefore instead of rewriting the logical code in the program, we write this logical code inside the function with the name provided to this function and using the same function name to call it in the program whenever the code is needed to be executed. In Perl programming, language function is also known as subroutine or method, which means the same, unlike in other programming languages, subroutines and functions and methods are different. In general, the Perl function is defined as a function containing subcodes or a logical set of code to perform some task and can be reused in the program.
Working of Perl functions with Examples
In this article, we will see how to declare and use the function in Perl programming. The function or subroutine in Perl is defined as the same as other functions in other programming languages. The subroutine contains a set of logical code that needs to be executed again and again in the program. Using subroutine, we call the subroutine when we want to execute this logical code where we need not write the entire logical code repeatedly. Hence the subroutines can be reused once written in the entire program. In Perl, subroutines or functions are created or declared using the “sub” keyword before the subroutine name which is given to the set of logical code that is reused in the program and in Perl, the Perl compiler first compiles the program and then executes it irrespective of declaration of subroutines or functions.
In Perl, the function or subroutines as in other programming languages we can pass any number of parameters to subroutines, and also we can pass lists, arrays, hashes to subroutines in Perl but returning the values, we can return array and hashes not more than one which may lead to ambiguity of identities of array or hashes.
Now let us see the syntax of defining the subroutines in Perl:
Syntax:
sub func_name
{
Statements or body of function;
}
In the above syntax, we can see whenever we are defining the subroutine; we first use the “sub” keyword before the subroutine or function name and the set of code that needs to be performed again and again is written within the curly brace, which is known as the body of subroutine or function. The above syntax is used for defining or declaring the subroutine.
Now let us see the syntax for calling the subroutines.
Syntax:
func_name(parameter_lst);
The above syntax differs if we are using Perl version below 5 as the above syntax for calling subroutine works only in Perl version 5 and above, and if we are using below 5 versions, then we need to use an ampersand (&) before func_name such as “&func_name(parameter_lst)” which is usually not used as it means as bypassing the subroutine prototypes and hence it is not recommended to use.
Now let us see a few examples of how to define function or subroutine in Perl:
Example #1
Code:
#!/usr/bin/perl
print "Demonstration of Perl function or subroutine:";
print "\n";
print "\n";
print "The function or subroutine is defined using sub.";
print "\n";
sub func_name {
print "Welcome to Educba with Perl tutorial";
print "\n";
}
print "The above defined function is now called ";
print "\n";
func_name();
Output:
In the above program, we can see we have defined subroutine or function using keyword “sub” before subroutine name, and in the above code, the subroutine name is “func_name”, and inside the function, we are just printing the message and then we are calling the subroutine using its name, and in the above code, we are defining the subroutine with no parameters.
Now we will see another example that uses parameters or arguments that are passed to the function or subroutine that is defined.
Example #2
Code:
#!/usr/bin/perl
print "Demonstration of Perl function or subroutine with arguments:";
print "\n";
print "\n";
print "The function is defined for calculating sum of two numbers passed as argument";
print "\n";
sub add_func
{
$op1 = $_[0];
print "The first number is given as: ";
print "\n";
print $op1;
print "\n";
$op2 = $_[1];
print "The second number is given as:";
print "\n";
print $op2;
print "\n";
return ($op1 + $op2);
}
$res_sum = add_func(6, 5);
print "The sum of the two numbers is as follows:";
print "\n";
print $res_sum;
Output:
In the above program, we can see we have defined a function that will calculate the sum of given numbers, and these numbers are passed as parameters to the function when it is called. So in the above code, we can see the two numbers that are passed are printed, and in the function, we are returning the sum of the two numbers using the return statement. Then when we call this function and pass any two numbers that need to be added.
Now we will see another example where we can pass list, array, and hashes as an argument.
Example #3
Code:
#!/usr/bin/perl
print "Demonstration of Perl function or subroutine with arguments:";
print "\n";
print "\n";
sub arr_func
{
print "The given array is as follows:";
print "\n";
print @arr1;
print "\n";
return;
}
sub lst_func
{
@list = @_;
print "The given list is as follows:";
print "\n";
print @list;
print "\n";
}
sub hash_func
{
(%hash) = @_;
foreach $key (keys %hash )
{
$val = $hash{$key};
print "$key : $val\n";
}
}
@arr1 = ("Educba ", "Institute ", "Goa");
@lst1 = (1, 2, 3, 4);
%hash1 = ('name' => 'Tom', 'age' => 19);
print "\n";
print "Passing array as parameter is";
print "\n";
arr_func(@arr1);
print "\n";
print "Passing list as parameter is";
print "\n";
lst_func(@lst1);
print "\n";
print "Passing hash as parameter is:";
print "\n";
hash_func(%hash1);
print "\n";
Output:
In the above program, we can see we have a defined array, list ad hash. In the above code, we can see we have used “@” for declaring list and array, and for declaring hash, we have used “%”. In the above code, we are just passing these array, list, and hash to the subroutines or function defined.
Conclusion
In this article, we conclude that function or subroutine in Perl is defined as a set of code that is used to perform some task again and again in the program, so to use this set of code, we write this code within the function or subroutine, and we call that function or subroutine by its name. This article also saw different examples of demonstrating how function or subroutine is defined by passing parameters, without passing parameters, passing lists, arrays, and hashes as parameters.
Recommended Articles
This is a guide to the Perl function. Here we discuss the introduction and working of Perl functions with examples for better understanding. You may also have a look at the following articles to learn more –