How to use subroutines in perl?

Asked By 10 points N/A Posted on -
qa-featured

Recently, I have started learning Perl programming.

Now, In my program I want to have a subroutine.

Where I can pass parameters and return values from that subroutine.

Can anyone tell me how to do that?

SHARE
Answered By 0 points N/A #160163

How to use subroutines in perl?

qa-featured

Try this:

For Subroutines
 
Like any sensible programming language Perl permits the user to outline their own functions, referred to as subroutines. they will be placed anywhere in your program however it's in all probability best to place all of them at the start or all at the top. A subroutine has the shape.
 
sub printargs
{
print "@_n";
}
 
&printargs("perl", "king"); # Example prints "perl king"
&printargs("frog", "and", "toad"); # Prints "frog and toad"
 
 
Regardless of any parameters that we may want to pass to it. All of the following will work to call this subroutine. Notice that a subroutine is called with an & character in front of the name:
 
&mysubroutine; # Call the subroutine
&mysubroutine($_); # Call it with a parameter
&mysubroutine(1+2, $_); # Call it with two parameters
In the higher than case the parameters are acceptable however ignored. When the subroutine is named any parameters are passed as a listing within the special @_ list array variable. This variable has completely nothing to try to with the $_ scalar variable. the subsequent subroutine just prints out the list that it had been referred to as with. it's followed by a handful of samples of its use.
sub printargs
{
print "@_n";
}
 
&printargs("perly", "king"); # Example prints "perly king"
&printargs("frog", "and", "toad"); # Prints "frog and toad"
Just like the other list array the individual parts of @_ is accessed with the sq. bracket notation:
sub printfirsttwo
{
print "Your first argument was $_[0]n";
print "and $_[1] was your secondn";
}
 
 
Again it ought to be stressed that the indexed scalars $_[0] and $_[1] and thus on don't have anything to with the scalar $_ which may even be used while not concern of a clash.
 
 
For returning values:
 
Result of a subroutine is usually the last item evaluated. This subroutine returns the most of 2 input parameters.
An example of its use follows.
 
Sub maximum
{
if ($_[0] > $_[1])
{
$_[0];
}
else
{
$_[1];
}
}
 
$biggest = &maximum(37, 24); # Now $biggest is 37
 
The &printfirsttwo subroutine higher than additionally returns a worth,during this case one.
this can be as a result of the last item that subroutine did was a print statement 
and therefore the results of a successful print statement is usually one.
 
For local variables:
 
The @_ variable is native to the present subroutine, and thus after all are $_[0], $_[1], $_[2], 
and so on. alternative variables is created native too, and this can be helpful if we wish to 
begin altering the input parameters. the subsequent subroutine tests to examine if one string is 
within another, areas not withstanding. An example follows.
 
sub inside
{
local($a, $b); # Make local variables
($a, $b) = ($_[0], $_[1]); # Assign values
$a =~ s/ //g; # Strip spaces from
$b =~ s/ //g; #   local variables
($a =~ /$b/ || $b =~ /$a/); # Is $b inside $a
#   or $a inside $b?
}
 
&inside("lemon", "dole money"); # true
 
Enable keep the basis phrase
 
local($a, $b) = ($_[0], $_[1]);
Hope this helps.

Login/Register to Answer

Related Questions