Updated April 5, 2023
Introduction to Perl Subroutine
Perl Subroutine is used to reusable code using a subroutine that we can use code, again and again, there is no need to repeatedly write the code. We can create our own functions it is called a subroutine. Subroutines are very important to improve the code reusability. It is also used to improve the code readability, using subroutine we can improve the code readability of our program. To use subroutine in our program we need to define or need to create it first, after creating then we call a subroutine in our code.
Syntax of Perl Subroutine
Below is the syntax are as follows:
1. Defining a subroutine: To use subroutine in our program we need to create it first and then we need to call in our program. Below syntax shows define or create a subroutine in perl are as follows.
Syntax:
Sub subroutine_name -- Name of subroutine that we have defining.
{
Statements; -- Statement to be used in body of the subroutine.
Return;
}
2. Calling Subroutine: In perl we calling subroutine by passing a list of arguments. Below syntax shows calling a subroutine in perl are as follows.
Syntax:
subroutine_name (arguments_list);
-- List of arguments which was used with subroutine.
Parameters of Perl Subroutine
Below is the parameter description syntax of the subroutine in Perl is as follows.
- Subroutine Name: It is very important while creating subroutine. We have defined any name to subroutine while creating the same. The subroutine name is the name that we have defined to the subroutine.
- Statement: The statement is defined as a block or body of the code of subroutine. This code is written under the body of the subroutine. We can define one or more statement in the body of the subroutines.
- Return: This is defined as the return an argument to its calling functions or subroutines. We can return no of arguments to the calling function in perl.
- Argument List: This is defined as arguments which we have used at the time of calling a subroutine in Perl. We have pass one or more arguments at the time of calling a subroutine in Perl.
How Does Subroutine Function work in Perl?
Below is the working of the subroutine is as follows. Below is the important use of subroutine in perl.
- Reusability of code.
- Improve the code readability.
- The subroutine is used to improve code reusability. If we want to take input from the user multiple times at the same time we have creating subroutine and then we call the subroutine in our program whenever we need it.
- We can improve the code reusability while using the subroutine in our program. After using subroutine there is no need to write it again and again.
- We can create our own functions it is called subroutines.
- To use subroutine in our program we need to define or need to create it first, after creating then we call the same in our code.
- It is used to reusability of code using subroutine we can use code, again and again, there is no need to write the code again and again.
- It is also used to improve the code readability, using subroutine in perl we can improve the code readability of our program.
- Subroutines is very important and useful to improve code reusability and readability.
- It or function is a group or statement that used together to perform a task. We can divide our code into separate subroutines.
- To divide subroutine into separate subroutines is depends on the developer but we can divide it logically based on the function which performed a specific task.
- In the subroutine, function and method are the same but in other languages this is different.
- The subroutines are used in perl programming language because subroutine in Perl created by using sub keyword.
- When we have called to function or subroutines in our program, perl compiler will stop its all executing program and goes to execute the function to execute the same and then return back to execute the remaining section code.
- We have avoided this by using the return statement in our program.
Examples
Below is the example of the subroutine in Perl is as follows.
Example #1
Passing Arguments to Subroutine: Below example shows passing arguments to a subroutine.
Code:
# Defining function in perl.
sub Average {
# Dispay number of arguments.
$number = scalar(@_);
$sum_of_num = 0;
foreach $item (@_) {
$sum_of_num += $item;
}
$average_of_num = $sum_of_num / $number;
print "Average of numbers : $average_of_num\n";
}
# Calling function average to define average of given numbers.
Average(100, 200, 300, 400, 500);
Output:
Example #2
Passing List to Subroutine: Below example shows passing a list to a subroutine.
Code :
# Defining function in perl with list as arguments.
sub sub_PrintList {
my @sub_list = @_;
print "Given list @sub_list\n";
}
$p = 1;
@q = (2, 3, 4, 5, 6, 7, 8, 9, 10); ## Provide list as user input.
# Function call with list parameter
sub_PrintList($p, @q);
Output:
Example #3
Passing Hashes to Subroutine: Below example shows passing a hashes to a subroutine. In the below example, we have to pass the hashes to the subroutine.
Code:
# Defining function in perl with hashes as arguments.
sub sub_PrintHash {
my (%sub_hash) = @_;
foreach my $key ( keys %sub_hash ) {
my $hash_value = $sub_hash{$key};
print "$key : $hash_value\n";
}
}
%sub_hash = ('name' => 'ABC', 'age' => 25);
# Function call with hashes parameter
sub_PrintHash(%sub_hash);
Output:
Example #4
Returning Value from a Subroutine: The below example shows returning a value from a subroutine. In the below example, we have to return value from a subroutine.
Code:
# Defining function in perl.
sub Average {
$number = scalar(@_);
$sum_number = 0;
foreach $item (@_) {
$sum_number += $item;
}
$average_num = $sum_number / $number;
return $average_num;
}
# Function call
$num = Average(10, 20, 30, 40, 50, 60, 70, 80, 90, 100);
print "Average for numbers : $num\n";
Output:
Conclusion
In perl we can create our own functions it is called as subroutines, subroutines is very important to improve the code reusability. It is used to reusability of code using subroutine we can use code again and again, there is no need to write the code again.
Recommended Articles
We hope that this EDUCBA information on “Perl Subroutine” was beneficial to you. You can view EDUCBA’s recommended articles for more information.