Passing Variable from user defined function in Perl

What is Passing Variable in a Function?


Passing Variable from user defined function is done by using the values declared outside a function, make the neccessary calculation inside the function and then return the result whenever the function is called.If you are passing variables to a function there is an array @_ which lists all the variables when the function is called, to improve the usability of a function.

Example :

#! C:programfilesperlbinperl
print "content-type: text/htmlnn";
$x = 12;
$y = 13;
$sum = add($x, $y);
print "The SUM is:",$sum;
sub add
my ($a, $b) = @_;
$c = $a + $b;
return $c;
Result :

The SUM is:25

In the above passing variable example the values of variables "$x=12","$y=13" is passed to the values "$a","$b" to do addition inside the function to store the values in "$c", the output is go outside the loop in the variable "$sum".

Ask Questions

Ask Question