Open In App

Perl | Multiple Subroutines

Last Updated : 29 May, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite: Perl | Subroutines or Functions

A Perl function or subroutine is a group of statements that together perform a specific task. In every programming language user want to reuse the code. So the user puts the section of code in function or subroutine so that there will be no need to write code again and again.
In Perl, the terms function, subroutine, and method are the same but in some programming languages, these are considered different. The word subroutines are used most in Perl programming because it is created using keyword sub. Whenever there is a call to the function, Perl stops executing all its program and jumps to the function to execute it and then returns back to the section of code that it was running earlier. One can avoid using the return statement.

Defining Subroutines:

The general form of defining the subroutine in Perl is as follows-

sub subroutine_name
{
    # body of method or subroutine
}

In Perl, a program can hold multiple subroutines with the same name without generating an error, because Perl allows to write multiple subroutines with the same name unless they have different Signatures. This can be defined by using different arity for each subroutine having the same name.

Arity of a Subroutine: Perl subroutines can have the same name unless they have a different set of Arity. Arity refers to the number of arguments that a subroutine contains. These arguments may or may not be of the different datatype. As long as the arity of subroutines differs from each other, the Perl program will not generate any error.

Use of ‘multi’ keyword:
Multiple subroutines in Perl can be created by using the keyword ‘multi’. This helps in the creation of multiple subroutines with the same name.

Example:

multi Func1($var){statement};
multi Func1($var1, $var2){statement1; statement2;}

Use of Multiple subroutines is very common in the creation of built-in functions and most of the operators in a Programming language like Perl. This helps in reducing the complexity of the program by not using different names for every other subroutine. Whatever code statement that is required, just pass the number of arguments required for that function and the work will be done. In this case, the compiler will pick the version of subroutine whose Function signature matches the one called for execution.

Various programs like Factorial of a number, Fibonacci series, etc. require more than one function to solve the problem. Use of Multiple subroutines will help reducing the complexity of such programs.

Example 1: Sum of Fibonacci series.




#!/usr/bin/perl
# Program to print sum of fibonacci series
  
# Function for $n = 0
multi Fibonacci_func(0)
{
    1; # returning 1
}
  
# Function for $n = 1
multi Fibonacci_func(1)
{
    1; # returning 1
}
  
# Recursive function to calculate Sum
multi Fibonacci_func(Int $n where $n > 1)
{
    Fibonacci_func($n - 2) + 
    Fibonacci_func($n - 1);
}
  
# Printing the sum 
# using Function Call
print Fibonacci_func(17);


Output:

2584

Above example uses multiple subroutines to calculate the Sum of Fibonacci Series.
 
Example 2: Factorial of a Number




#!/usr/bin/perl
# Program to print factorial of a number
  
# Factorial of 0
multi Factorial(0)
{
    1; # returning 1
}
  
# Recursive Function 
# to calculate Factorial
multi Factorial(Int $n where $n > 0)
{
    $n * Factorial($n - 1); # Recursive Call
}
  
# Printing the result 
# using Function Call
print Factorial(15);


Output:

3628800

In the above-given Examples, the program uses the ‘multi’ keyword to declare multiple subroutines with the same name but with different arity.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads