- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Define and Call a Subroutine in Perl
The general form of a subroutine definition in Perl programming language is as follows −
sub subroutine_name { body of the subroutine }
The typical way of calling that Perl subroutine is as follows −
subroutine_name( list of arguments );
In versions of Perl before 5.0, the syntax for calling subroutines was slightly different as shown below. This still works in the newest versions of Perl, but it is not recommended since it bypasses the subroutine prototypes.
&subroutine_name( list of arguments );
Let's have a look into the following example, which defines a simple function and then call it. Because Perl compiles your program before executing it, it doesn't matter where you declare your subroutine.
Example
#!/usr/bin/perl # Function definition sub Hello { print "Hello, World!\n"; } # Function call Hello();
Output
When the above program is executed, it produces the following result −
Hello, World!
- Related Articles
- Subroutine Call Context in Perl
- Private Variables in a Subroutine in Perl
- Passing Arguments to a Subroutine in Perl
- Returning Value from a Subroutine in Perl
- Define a Pagination in Perl
- Define a Report Footer in Perl
- How to Define a Format in Perl?
- How to declare, define and call a method in Java?
- How to define and call a function in Lua Programming?
- Call by value and Call by reference in Java
- 8085 program with a subroutine to add ten packed BCD numbers.
- Single and Double Quotes in Perl
- Current Date and Time in Perl
- Format Date and Time in Perl
- Opening and Closing Files in Perl

Advertisements