# Perl Subroutines - Perl Scripting

## What are Subroutines in Perl? What are its functions?

As we have seen in other programming languages like Sap Abap, java XML about subroutines, similarly in Perl subroutine or function is a set of statements that perform their task together. You have an option here to divide your code into separate, small subroutines. It is your duty as to how you divide up your code among different subroutines, but as a matter-of-fact logically the division occurs and each function performs a specific task.
Perl uses the term subroutine, method and function interchangeably.

## Define and Call a Subroutine

The general form of a subroutine definition in Perl programming language is as follows −
The typical way of calling that Perl subroutine is as follows −
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 to use since it bypasses the subroutine prototypes.
Now, look at the following example, which defines a simple function and then call it. Because Perl compiles your program before executing it, it doesn't require where you declare your subroutine.
When the above program is executed, it produces the following result −

## Passing Arguments to a Subroutine

Passing arguments to a subroutine can be done like in any other programming language and they can be accessed inside the function using the special array @_. Thus the first argument to the function is in $_[0], the second is in$_[1], and so on.
You can pass arrays and hashes as arguments to subroutines like any other scalar. But passing more than one array or hash normally inclined to lose their separate identities. So references are used(explained in the next chapter ) to pass any array or hash.
Let's try the following example, which takes a list of numbers and then prints their average −
When above program is executed, it produces the following result −

## Passing Lists to Subroutines

Here @_ variable is an array, so it can be used to supply lists to a subroutine. However, because of the way in which Perl accepts and parses lists and arrays, it is very difficult to extract the individual elements from @_. If you want to pass a list along with other scalar arguments, then make list as the last argument as shown below −
When above program is executed, it produces the following result −

## Passing Hashes to Subroutines

When you pass a hash to a subroutine or operator that accepts a list, then hash will automatically get translated into a list of key/value pairs. For example −
When above program is executed, it produces the following result −

## Returning Value from a Subroutine

Here in Perl the user can return a value from subroutine like we do in any other programming language. If the value is not returned from a subroutine then whatever calculation is performed at last in a subroutine is automatically becomes the return value.
You also have an option to return arrays and hashes from the subroutine like any scalar but returning more than one array or hash normally causes them to lose their separate identities. So use of references is suggested( explained in the next chapter ) to return any array or hash from a function.
Let's try the following example, which takes a list of numbers and then returns their average −
When above program is executed, it produces the following result −

## How many types of variables are there in a subroutine?

By default, all variables in Perl are global variables as we have discussed earlier, which means they can be accessed through anywhere in the program. But, here you can create private variables called lexical variables at any time with the my operator option.
The my operator used to confine a variable to a particular region of code in which it can be used and accessed. Beyond that region, this variable cannot be used or accessed. This region is called its scope. A lexical scope is usually a block of code with a set of braces around it, such as those defining the body of the subroutine or those marking the code blocks of if, while, for, foreach, and evalstatements.
Following is an example that shows you how to define a single or multiple private variables using my operator −
Let's check the following example to distinguish between global and private variables −
When above program is executed, it produces the following result −

## Temporary Values via local()

The local is mostly used when the current value of a variable must be visible to call subroutines. A local just supplies temporary values to global (meaning package) variables. This is known as dynamic scoping. Lexical scoping is done with my operator option, which works more like C's auto declarations.
If more than one variable or expression is given to local, they should be placed in parentheses. This operator works perfectly by saving the current values of those variables in its argument list on a hidden stack and again restoring them upon by exiting the block, subroutine, or eval.
Let's check the following example to distinguish between global and local variables −
When above program is executed, it produces the following result −

## State Variables via state()

Another type of lexical variables are available in Perl, which are similar to that of private variables but they maintain their state perfectly and they do not get reinitialized or rely upon multiple calls of the subroutines. These variables are defined using the state operator and available starting from Perl 5.9.4.
Let's check the following example to demonstrate the use of state variables −
When above program is executed, it produces the following result −
Prior to Perl 5.10, you would have to write it like this −

## Subroutine Call Context

The context of a subroutine or statement is defined as the type of return value that is expected to call. This allows the user to use a single function that returns different values based on what the user is expecting to receive. For example, the following local time () returns a string when it is called in scalar context, but it returns a list when it is called in list context, depending upon the type of function it returns.
In this example, the value of \$timestr is now a string made up of the current date and time, for example, Thu Nov 30 15:21:33 2000. Conversely −
Now here you can see the individual variables that contain the corresponding values returned by local time () subroutine.