next up previous contents
Next: Using the Parameter Array Up: Practical Perl Programming Previous: The foreach statement

Functions

This chapter takes a look at functions. Functions are blocks of codes that are given names so that you can use them as needed. Functions help you to organize your code into pieces that are easy to understand and work with. They let you build your program step by step, testing the code along the way.

After you get the idea for a program, you need to develop a program outline-either in your head or on paper. Each step in the outline might be one function in your program. This is called modular programming. Modular programming is very good at allowing you to hide the details so that readers of your source code can understand the overall aim of your program.

For instance, if your program has a function that calculates the area of a circle, the following line of code might be used to call it:

$areaOfFirstCircle = areaOfCircle($firstRadius);

By looking at the function call, the reader knows what the program is doing. Detailed understanding of the actual function is not needed.

Note Calling a function means that Perl stops executing the current series of program lines. Program flow jumps into the program code inside the function. When the function is finished, Perl jumps back to the point at which the function call was made. Program execution continues from that point onward.

Let's look at the function call a little closer. The first thing on the line is a scalar variable and an assignment operator. You already know this means Perl assigns the value on the right of the assignment operator to $areaOfFirstCircle.

But, what exactly is on the right?

The first thing you see is the function name areaOfCircle(). The parentheses directly to the right and no $, @, or % beginning the name indicates that this is a function call. Inside the parentheses is a list of parameters or values that get passed to the function. You can think of a parameter just like a football. When passed, the receiver (for example, the function) has several options: run (modify it in some way), pass (call other routines), fumble (call the error handler).

Note Perl enables you to use the & character to start function names, and in a few cases it is needed. Those few situations that the & character is needed are beyond the scope of this book.

Here is an example call of the function, func1/pl:

$areaOfFirstCircle = areaOfCircle(5);

print("$areaOfFirstCircle\n");



sub areaOfCircle {

    $radius = $_[0];

    return(3.1415 * ($radius ** 2));

}

This program prints:

78.7375

The fact that something prints tells you that the program flow returned to the print line after calling the areaOfCircle() function.

A function definition is very simple. It consists of:

sub functionName {

                  }

That's it. Perl function definitions never get any more complex.

The complicated part comes when dealing with parameters. Parameters are values passed to the function (remember the football?). The parameters are specified inside the parentheses that immediately follow the function name. In example above, the function call was areaOfCircle(5). There was only one parameter, the number 5. Even though there is only one parameter, Perl creates a parameter array for the function to use.

Inside the areaOfCircle() function, the parameter array is named @_. All parameters specified during the function call are stored in the @_ array so that the function can retrieve them. Our small function did this with the line:

$radius = $_[0];

This line of code assigns the first element of the @_ array to the $radius scalar.

Note Because parameters always are passed as lists, Perl functions also are referred to as list operators. And, if only one parameter is used, they are sometimes referred to as unary operators. However, I'll continue to call them functions and leave the finer points of distinction to others.

The next line of the function:

return(3.1415 * ($radius ** 2));

calculates the circle's area and returns the newly calculated value. In this case, the returning value is assigned to the $areaOfFirstCircle scalar variable.

Note If you prefer, you don't need to use the return() function to return a value because Perl automatically returns the value of the last expression evaluated. I prefer to use the return() function and be explicit so that there is no mistaking my intention.

You may have used programming languages that distinguish between a function and a subroutine, the difference being that a function returns a value and a subroutine does not. Perl makes no such distinctions. Everything is a function-whether or not it returns a value.



 
next up previous contents
Next: Using the Parameter Array Up: Practical Perl Programming Previous: The foreach statement
dave@cs.cf.ac.uk