© William "Bo" Rothwell of One Course Source, Inc. 2019
William "Bo" RothwellBeginning Perl Programminghttps://doi.org/10.1007/978-1-4842-5055-6_12

12. Functions

William “Bo” Rothwell1 
(1)
San Diego, CA, USA
 

Creating Functions

To create a function, use the sub statement:
sub total {
  print "The total is ", $a * $b + $c * $d, "\n";
}
Notes:
  • Functions are most often created for either breaking up a large script into “components” or for when code is used more than once within a script.

  • Be careful when choosing function names. Perl has many built-in functions (see how to avoid confusion in the “Invoking Functions” section).

  • Functions cannot be “redeclared” because they are created at compile time, not run time.

  • Functions can be placed anywhere in your Perl script, ...

Get Beginning Perl Programming: From Novice to Professional 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.