I use capitals to indicate the variable plays the role of a constant - it shouldn't be assigned to. Some people will suggest using use constant PI => 2 * atan2(1, 0), but that style gives constants that are harder to interpolate, so I tend to shy away from them.my $PI = 2 * atan2(1, 0);
I also note that both subroutines are almost the same. I would use just one subroutine, and an extra parameter. Something like:
I didn't run or even compiled the code above, so it may be full of typos.my $PI = 2 * atan2(1, 0); my $A = 1.18; my $C = 0.28e6; my $HWM = 2e-5; my $SCALE = 1e-6; sub sigma; my (@rads, @sigs); for (my $i = 1; $i <= 100; $i += 2)) { my $r = $i * 1e-6; push @rads, $r; push @sigs, sigma($r, $r >= $HWM) } print "radii: @rads\n"; print "sigmas: @sigs\n"; sub sigma { my ($rad, $high) = @_; $PI * $rad ** 2 * ($high ? 2 : $A * (1 - exp(-$C * $rad))); }
In reply to Re: Improve my Code: Subroutines
by JavaFan
in thread Improve my Code: Subroutines
by cheech
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |