Wednesday, September 06, 2017

Perl Subroutine Arguments

Subroutines:-

1. Retrieving arguments to a subroutine with shift:

A subroutine's arguments come in via the special @_ array. The shift without an argument defaults to @_.

   sub volume {
       
my $height = shift;
       
my $width = shift;
       
my $depth = shift;

        return $height
* $width * $depth;
   
}


2. Assign arguments to a subroutine with list assignment:

You can also assign arguments en masse with list assignment:

   sub volume {
       
my ($height, $width, $depth) = @_;

        return $height
* $width * $depth;
   
}

3. Handle arguments directly by accessing @_:

In some cases, but we hope very few, you can access arguments directly in the @_ array.

   sub volume {
        return $_
[0] * $_[1] * $_[2];
   
}


references:

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.