Subroutines are like functions in C language.We can define a subroutine by writing sub and then variable name.
For example:
sub read{
$line=<STDIN>;
}
Above is a subroutine which is used to read input from Standard Input Device (such as Keyboard).
When we have to call the subroutine we add & before that subroutine name.
For Example:
&read;
Output:
2)Local Variables in Subroutine:
Output:
3)Subroutine with Arguments:
A subroutine can have arguments as well.We can accept arguments in subroutine by using the following statement:
my($num1,$num2)=@_;
where num1 and num2 are local variables for this subroutine.@_ is the placeholder.
Output:
4)Subroutine Forward Reference:
If we will declare the subroutine before calling it then there is no need to & at the time of subroutine calling.
For example:
sub read{
$line=<STDIN>;
}
Above is a subroutine which is used to read input from Standard Input Device (such as Keyboard).
When we have to call the subroutine we add & before that subroutine name.
For Example:
&read;
Program Code:
#!/Perl/bin/perl
print("Enter a sentence\n");
while(1)
{
&read;
last if($line eq "");
print($line);
}
sub read{
$line=<STDIN>;
}
print("done");
#!/Perl/bin/perl
print("Enter a sentence\n");
while(1)
{
&read;
last if($line eq "");
print($line);
}
sub read{
$line=<STDIN>;
}
print("done");
Output:
2)Local Variables in Subroutine:
Program Code:
#! /Perl/bin/perl
print("I have declared a local variable and it can't be accessed from outside the subroutine\n");
$calc=&addition;
print("The value returned by subroutine is $calc \n");
print("The value of local variable outside the subroutine is $total \n Since we are accessing it from outside the subroutine\n");
sub addition
{
my($total)=50;
return $total;
}
#! /Perl/bin/perl
print("I have declared a local variable and it can't be accessed from outside the subroutine\n");
$calc=&addition;
print("The value returned by subroutine is $calc \n");
print("The value of local variable outside the subroutine is $total \n Since we are accessing it from outside the subroutine\n");
sub addition
{
my($total)=50;
return $total;
}
Output:
3)Subroutine with Arguments:
A subroutine can have arguments as well.We can accept arguments in subroutine by using the following statement:
my($num1,$num2)=@_;
where num1 and num2 are local variables for this subroutine.@_ is the placeholder.
Program Code:
#! /Perl/bin/perl
print("Enter first number\n");
$first=<STDIN>;
print("Enter second number\n");
$second=<STDIN>;
$calc=&addition($first,$second);
print("The sum of numbers is $calc \n");
sub addition
{
my($num1,$num2)=@_;
$total=$num1+$num2;
}
#! /Perl/bin/perl
print("Enter first number\n");
$first=<STDIN>;
print("Enter second number\n");
$second=<STDIN>;
$calc=&addition($first,$second);
print("The sum of numbers is $calc \n");
sub addition
{
my($num1,$num2)=@_;
$total=$num1+$num2;
}
Output:
4)Subroutine Forward Reference:
If we will declare the subroutine before calling it then there is no need to & at the time of subroutine calling.
Program Code:
#! /Perl/bin/perl
print("Enter a sentence\n");
sub read; #forward reference
while(1)
{
#now there is no need to use & before subroutine name but sometimes it doesn't work
&read;
last if($line eq "");
print($line);
}
sub read{
$line=<STDIN>;
}
print("done");
Output:#! /Perl/bin/perl
print("Enter a sentence\n");
sub read; #forward reference
while(1)
{
#now there is no need to use & before subroutine name but sometimes it doesn't work
&read;
last if($line eq "");
print($line);
}
sub read{
$line=<STDIN>;
}
print("done");
0 comments:
Post a Comment