How to set Default value for a subroutine argument and how many ways we can do it?
subroutines variable may have more than one number of arguments, some arguments may get missing values, providing default values for arguments is mandatory.
Method1:
sub add
{
($v1,$v2)=@_;
$v2 ||=1;
}
Method2:
sub add
{
($v1,$v2)=@_;
if(!defined $v2)
{
$v2=1;
}
}
Method3:
sub add
{
$v1=shift @_;
if($#_ > 0){
$v2=@_[1];
}
else
{
$v2=1;
}
Method1:
sub add
{
($v1,$v2)=@_;
$v2 ||=1;
}
Method2:
sub add
{
($v1,$v2)=@_;
if(!defined $v2)
{
$v2=1;
}
}
Method3:
sub add
{
$v1=shift @_;
if($#_ > 0){
$v2=@_[1];
}
else
{
$v2=1;
}
0 Comments:
Post a Comment
Note: only a member of this blog may post a comment.
<< Home