Prerequisite: Subroutines or Functions 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 this article we will discuss the following concepts:
- Passing Hashes to Subroutines
- Passing Lists to Subroutines
- Returning Value from a Subroutine
- Local and Global Variables in Subroutines
- Different number of parameters in subroutine call
Passing Hashes to Subroutines: A hash can also be passed to subroutines which automatically converted into its key-value pair.
Marks : 97 Subject : Perl
Passing Lists to Subroutines: As we know that @_ is a special array variable inside a function or subroutine, so it is used to pass the lists to the subroutine. Perl has a different way to accept and parse arrays and lists that make it difficult to extract the discrete element from @_. In order to pass a list along with other scalar arguments, it is necessary to make the list as the last argument.
Given list is 100 10 20 30 40
Returning Value from a Subroutine: A subroutine can also return a value like in other programming languages such as C, C++, Java etc. If the user will not return a value from subroutine manually, then the subroutine will return a value automatically. In this, the automatically returned value will be the last calculation executed in the subroutine. The return value may be scalar, array or a hash.
Sum of the given numbers : 72
Local and Global Variables in Subroutines: All the variables inside a Perl program are Global vairbales by default. But with the help of my keyword, you can create the local or private variables inside a block. A private variable has a limited scope like between the block(if, while, for, foreach etc.) and methods etc. Outside block or method, these variables can’t be used.
Inside the Subroutine: GFG Outside the Subroutine: GeeksforGeeks
Different number of parameters in subroutine call: Perl does not provide us any built-in facilities to declare the parameters of a subroutine, which makes it very easy to pass any number of parameters to a function.
Note: Generally, passing more than one array or hash as parameters to subroutines causes them to lose their separate identities. Similarly, returning more than one array or hash from subroutine also causes to lose their separate identities. We can solve these problems by using references.
- Perl | Subroutines or Functions
- Perl - Difference between Functions and Subroutines
- Perl | Multiple Subroutines
- Perl | Recursive Subroutines
- Perl | Basic Syntax of a Perl Program
- Perl Tutorial - Learn Perl With Examples
- Perl | String functions (length, lc, uc, index, rindex)
- Perl | List Functions
- Perl | Useful String functions
- Perl | Useful Array functions
- Perl | Useful Math functions
- Perl | Useful Hash functions
- Perl | Useful File-handling functions
- Perl | File I/O Functions
- Perl | Operators | Set - 1
- Perl | Operators | Set - 2
- Perl | DBI(Database Independent Interface) Module Set - 1
- Perl - DBI(Database Independent) Module | Set - 2
- Perl | split() Function
- Perl | chomp() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.