A Note About Using Functions

Now that you've learned about the print and chomp functions, as well as had a glimpse of other functions such as int and sprintf, this is a good time to go over how function calls work. If you've been paying careful attention, you might have noticed that I've been calling the print function like this:

print "Hello, World!\n";

I've also been calling chomp and printf like this:

chomp($input = <STDIN>);
printf("Average (mean): %.2f", $avg);

One form has parentheses around the arguments to the function, the other form doesn't. Which is correct? The answer is both. Parentheses around the arguments to a function are optional; as long as Perl can figure out what your arguments are, either form works just fine.

In this ...

Get Sams Teach Yourself Perl in 21 Days, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.