Robert's Perl Tutorial

http://www.sthomas.net/roberts-perl-tutorial.htm


Parameters

One solution is to pass the subroutine parameters. In the best tradition of American columnists, who seem to have a particular affection for this phrase, 'Here's how:'

($height,$ratio)=@ARGV;
$cnv1=3.2;			

&howfar($height,$ratio);
print "With a glide ratio of $ratio:1 you can fly $distance from $height\n";

&howfar($height,$ratio+1);
print "With a glide ratio of ",$ratio+1,":1 you can fly $distance from $height\n";

&howfar($height,$ratio-1);
print "With a glide ratio of ",$ratio-1,":1 you can fly $distance from $height\n";

sub howfar {
	print "The parameters passed to this subroutine are @_\n";
	($ht,$rt)=@_;
	$ht  =int($ht/$cnv1);
	$distance=int($ht*$rt);
}

Quite a few things have changed here. Firstly, the subroutine is being called with parameters. These are a comma-delimited list in parens after the subroutine call. The two parameters are $height and $ratio.

The parameters end up in the subroutine as the @_ array. Being an array, they are in the same order as passed. All the usual array operations work. All we will do is assign the contents of the array to two variables.

We have also moved the conversion function into the subroutine, because we want to put all the code for generating the distance into one place.