Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Practice Problems
  • Python
  • C
  • C++
  • Java
  • Courses
  • Machine Learning
  • DevOps
  • Web Development
  • System Design
  • Aptitude
  • Projects
Open In App
Next Article:
Perl | Scope of a Subroutine
Next article icon

Perl | Multiple Subroutines

Last Updated : 29 May, 2019
Comments
Improve
Suggest changes
Like Article
Like
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.



Next Article
Perl | Scope of a Subroutine

A

Abhinav96
Improve
Article Tags :
  • Perl
  • Perl-function

Similar Reads

  • 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 func
    2 min read
  • Perl | Recursive Subroutines
    Prerequisite: Recursion in PerlRecursive means about or using a rule or procedure that can be applied repeatedly. It is the process of defining a function or calculating a number by the repeated application of an algorithm. Recursive Subroutine is a type of subroutine that calls itself as part of it
    7 min read
  • Perl | Scope of a Subroutine
    Subroutines in Perl, are reusable unit of code. It is a dynamic concept. Functions and subroutines are the two terms that can be used interchangeably. If you want to be strict on the semantics, small pieces of named blocks of code that accept arguments and return values are called subroutines. The b
    7 min read
  • Perl | Slurp Module
    The File::Slurp module is used to read contents of a file and store it into a string. It is a simple and efficient way of Reading/Writing/Modifying complete files. Just like its name, it allows you to read or write entire files with one simple call. By importing this module to your program, the user
    4 min read
  • Perl | substitution Operator
    Substitution Operator or 's' operator in Perl is used to substitute a text of the string with some pattern specified by the user. Syntax: s/text/pattern Returns: 0 on failure and number of substitutions on success Example 1: #!/usr/bin/perl -w # String in which text # is to be replaced $string =
    1 min read
  • Perl | rindex() Function
    rindex() function in Perl operates similar to index() function, except it returns the position of the last occurrence of the substring (or pattern) in the string (or text). If the position is specified, returns the last occurrence at or before that position. Syntax: # Searches pat in text from given
    2 min read
  • Perl | substr() function
    substr() in Perl returns a substring out of the string passed to the function starting from a given index up to the length specified. This function by default returns the remaining part of the string starting from the given index if the length is not specified. A replacement string can also be passe
    2 min read
  • Perl | References to a Subroutine
    Prerequisite: Perl references Declaring References to a Subroutine In Perl, a reference is, exactly as the name suggests, a reference or pointer to another object. References actually provide all sorts of abilities and facilities that would not otherwise be available and can be used to create sophis
    6 min read
  • Perl | sprintf() Function
    sprintf() function in Perl uses Format provided by the user to return the formatted string with the use of the values in the list. This function is identical to printf but it returns the formatted string instead of printing it. Syntax: sprintf Format, List Returns: a formatted scalar string Example
    1 min read
  • Perl | Passing Complex Parameters to a Subroutine
    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, the user wants to reuse the code. So the user puts the section of code in a function or subroutine so that there will be no need
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences