Hour 8. Functions

 <  Day Day Up  >  

What You'll Learn in This Hour:

  • How to define your own functions and call them

  • How to pass values into functions and return values

  • How to write programs with use strict to enforce structure

Almost all computer languages support functions . A function is a grouping of code statements that can be called by name to do some work and then return some value. You've been using functions throughout this book; for example, you've used print , chomp , sort , open , close , split , and so on. These functions are built into Perl.

Perl also allows you to write your own functions. In Perl, user -defined functions are called subroutines , or subs . Like Perl's built-in functions, user-defined functions can take arguments and return values to the caller.

Perl also supports the concept of scope . Scope determines the set of variables that a given statement in the program can access as it is executed. Because of Perl's scope features, you can write functions that can behave autonomously from the rest of your program. Well-written functions can be reused in other programs.

 <  Day Day Up  >  


SAMS Teach Yourself Perl in 24 Hours
Sams Teach Yourself Perl in 24 Hours (3rd Edition)
ISBN: 0672327937
EAN: 2147483647
Year: 2005
Pages: 241

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net