Saturday, October 18, 2008

Arguments are passed by reference

#!/usr/bin/perl -w
$v = 32;
print ("Value before calling the function: " . $v . "\n");
fun1($v);
print ("Value after calling the function: " . $v . "\n");
# end of program
sub fun1
{
print ("Value within the function: " . $_[0] . "\n");
$_[0] <<= 1; # shifts all bits left by 1 (like * 2)
print ("Value within the function, again: " . $_[0] . "\n");
}

No comments: