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 | Multiple Subroutines
Next article icon

Perl | Subroutines or Functions

Last Updated : 13 Jul, 2018
Comments
Improve
Suggest changes
Like Article
Like
Report
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 is used most in Perl programming because it is created using keyword sub. Whenever there is a call to the function, Perl stop 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  }  
Calling Subroutines: In Perl subroutines can be called by passing the arguments list to it as follows-
subroutine_name(aruguments_list);
The above way of calling the subroutine will only work with Perl version 5.0 and beyond. Before Perl 5.0 there was another way of calling the subroutine but it is not recommended to use because it bypasses the subroutine prototypes.
&subroutine_name(aruguments_list);
Example: perl
# Perl Program to demonstrate the  # subroutine declaration and calling  #!/usr/bin/perl  # defining subroutine sub ask_user {    print "Hello Geeks!\n"; }  # calling subroutine # you can also use # &ask_user(); ask_user(); 
Output:
Hello Geeks!
Passing parameters to subroutines: This is used to pass the values as arguments.This is done using special list array variables '$_'. This will assigned to the functions as $_[0], $_[1] and so on. Example: perl
# Perl Program to demonstrate the  # Passing parameters to subroutines  #!/usr/bin/perl  # defining subroutine sub area  {     # passing argument         $side = $_[0];          return ($side * $side); }  # calling function $totalArea = area(4);  # displaying result printf $totalArea; 
Output:
16
Advantage of using subroutines:
  • It helps us to reuse the code and makes the process of finding error and debug easy.
  • It helps in organizing the code in structural format.Chunks of code is organized in sectional format.
  • It increases the code readability.

Next Article
Perl | Multiple Subroutines

V

vaishali bhatia
Improve
Article Tags :
  • Misc
  • Perl
  • Perl-method
Practice Tags :
  • Misc

Similar Reads

  • Perl | Multiple Subroutines
    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
    3 min read
  • Perl | sin() Function
    This function is used to calculate sine of a VALUE or $_ if VALUE is omitted. This function always returns a floating point. Syntax: sin(VALUE) Parameters: VALUE in the form of float Returns: Function returns sine of VALUE. Example 1: #!/usr/bin/perl # Calling sin() function $var = sin(5); # Printin
    1 min read
  • Perl | sqrt() Function
    Many times it happens that while solving mathematical expressions we require to calculate the square root of a number. To solve this issue, like other programming language Perl provides us with a built-in function sqrt() which can be used to calculate the square root of a number. Syntax: sqrt value
    1 min read
  • Perl | srand() Function
    The srand() function in Perl helps rand() function to generate a constant value each time the program is run. This srand() function uses the same parameter each time the rand() function is called for getting constant random value. Syntax: srand(seed) Parameters: seed : It is an integer value. Return
    3 min read
  • Perl | Useful String functions
    A string in Perl is a scalar variable and start with a ($) sign and it can contain alphabets, numbers, special characters. The string can consist of a single word, a group of words or a multi-line paragraph. The String is defined by the user within a single quote (‘) or double quote (“). Perl provid
    3 min read
  • Perl | ord() Function
    The ord() function is an inbuilt function in Perl that returns the ASCII value of the first character of a string. This function takes a character string as a parameter and returns the ASCII value of the first character of this string. Syntax: ord string Parameter: This function accepts a single par
    1 min read
  • Function Signature in Perl
    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
    4 min read
  • Perl | return() Function
    return() function in Perl returns Value at the end of a subroutine, block, or do function. Returned value might be scalar, array, or a hash according to the selected context. Syntax: return Value Returns: a List in Scalar Context Note: If no value is passed to the return function then it returns an
    2 min read
  • Perl | prototype() Function
    prototype() function in Perl returns a string containing the prototype of the function or reference passed to it as an argument, or undef if the function has no prototype. Syntax: prototype(function_name) Parameter: function_name: Function whose prototype is to be determined Returns: prototype of th
    1 min read
  • Perl | tell() Function
    tell() function in Perl is used to get the position of the read pointer in a File with the use of its FileHandle. If no FileHandle is passed then it returns the position within the most recent accessed file. Syntax: tell(FileHandle) Parameter: FileHandle: Filehandle of the file to be accessed. Retur
    1 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