Previous Page Next Page

Chapter 11. How Do Subroutines Function?

11.1. Subroutines/Functions

In addition to the large number of Perl functions already available, you can create your own functions or subroutines. Some languages distinguish between the terms function and subroutine. Perl doesn't. If you say "subroutine," everyone will know you are talking about a "function," and if you say "function," everyone will know you are talking about a "subroutine."[1] Technically, a function is a block of code that returns a value, whereas a subroutine is a block of code that performs some task, but doesn't return anything. Perl subroutines and functions can do both, so we'll use the two terms interchangeably in this text. For now, we'll use the term "subroutine" when referring to user-defined functions.

[1] You could even change the title of this chapter to, "How Do Functions Subroutine?" but some people might frown.

Subroutines are self-contained units of a program designed to accomplish a specified task, such as calculating a mortgage payment, retrieving data from a database, or checking for valid input. When a subroutine is called in a program, it is like taking a detour from the main part of the program. Perl starts executing the instructions in the subroutine and when finished, returns to the main program and picks up where it left off. Subroutines can be used over and over again and thus save you from repetitious programming. They are also used to break up a program into smaller modules to keep it better organized and easier to maintain.

The subroutine declaration consists of one or more statements enclosed in a block, independent of your program and not executed until it is called. It is often referred to as a "black box." Information goes into the black box as input (like the calculator or remote control when you push buttons), and the action or value returned from the box is its output (such as a calculation or a different channel). What goes on inside the box is transparent to the user. The programmer who writes the subroutine is the only one who cares about those details. When you use Perl's built-in functions, such as print() or rand(), you send a string of text or a number to the function, and it sends something back. You don't care how it does its job; you just expect it to work. If you send bad input, you get back bad output or maybe nothing; hence the expression "Garbage in, garbage out."

The scope of a subroutine is where it is visible in the program. Subroutines are global and can be placed anywhere in the script, even in another file. When coming from another file, they are loaded into the script with the do, require, or use keywords. All variables created within a subroutine or accessed by it are also global unless specifically made local with either the local or my operators.

The subroutine is called, or invoked, by prefixing the subroutine name with an ampersand (&), by prefixing the subroutine with the do function,[2] or by appending a set of empty parentheses to the subroutine name. If a forward reference is used, neither ampersands nor parentheses are needed to call the subroutine.

[2] The primary use of the do function was to include Perl subroutines from the Perl 4 library (e.g., do'pwd.pl').

If a nonexistent subroutine is called, the program quits with an error message: Undefined subroutine in "main::prog" .... If you want to check whether the subroutine has been defined, you can do so with the built-in defined function.

The return value of a subroutine is the value of the last expression evaluated (either a scalar or an array). The return function can be used explicitly to return a value or to exit from the subroutine early due to the result of testing some condition.

If the call to the subroutine is made part of an expression, the returned value can be assigned to a variable, thus emulating a function call.

Format

Subroutine declaration:
    sub subroutine_name;
Subroutine definition:
    sub subroutine_name { Block }
Subroutine call:
    do subroutine_name;
    &subroutine_name;
    subroutine_name();
    subroutine_name;
Subroutine call with parameters:
    &subroutine_name(parameter1, parameter2, ... )
    subroutine_name(parameter1, parameter2, ... )


11.1.1. Defining and Calling a Subroutine

A declaration simply announces to the Perl compiler that a subroutine is going to be defined in the program and may take specified arguments. Declarations are global in scope; in other words, they are visible no matter where you put them in the program although it is customary to put declarations at the beginning or end of the program, or in another file. A subroutine definition is a block of statements that follows the subroutine name. A subroutine that has not been explicitly declared is declared at the same time it is defined.

Declaration:sub name_of_subroutine;
Definition:sub name_of_subroutine { statement; statement; }


A subroutine can be defined anywhere in your program or even in another file. The subroutine consists of the keyword sub followed by an opening curly brace, a set of statements, and ending in a closing curly brace.

The subroutine and its statements are not executed until called. You can call a subroutine by preceding its name with an ampersand or by attaching a set of empty parentheses after its name or by doing neither and calling it as a built-in function. If you call the subroutine with neither the ampersand nor parentheses, then you must declare it first.

Example 11.1.

(The Script)
1   sub greetme { print "Welcome, Välkommen till, Bienvenue!\n";}
2   &greetme if defined &greetme;
3   print "Program continues....\n";
4   &greetme; # Call to subroutine
5   print "More program here.\n";
6   &bye;
7   sub bye { print "Bye, adjo, adieu.\n"; }
8   &bye;

(Output)
2   Welcome, Välkommen till, Bienvenue!
3   Program continues....
4   Welcome, Välkommen till, Bienvenue!
5   More program here.
    Bye, adjo, adieu.
    Bye, adjo, adieu.

Explanation

  1. This is a subroutine definition consisting of the keyword sub, followed by the name of the subroutine, greetme, and a block of statements that will be executed when the subroutine is called. Officially, the subroutine name is preceded by an ampersand (&), but the only time you really need the ampersand is when calling the subroutine or when using its name to create a reference or as an argument to a function, such as defined. This definition can be placed anywhere in your program and will do nothing until it is called. In this example, there is only one print statement that will be executed when the function is called.

  2. The subroutine greetme is called by placing an ampersand (&) in front of its name. The term "invoke" a subroutine is often used to mean "call" the subroutine.The defined built-in function is used to check that the subroutine has been defined. When using the subroutine's name, the ampersand is required. When called, the program will jump into the subroutine and start executing the statements defined there; in this case, the Welcome statement.

  3. After the subroutine is called (invoked), program execution starts at the line right after where it was called and continues from there.

  4. The subroutine greetme is called again.

  5. The program resumes execution after the subroutine exits on line 4.

  6. The subroutine bye is called. The definition is found later on line 7.

  7. Subroutine bye is defined. No matter where subroutines are placed, the compiler sees them.

  8. Subroutine bye is called.

A Null Parameter List

If the subroutine name is followed by parentheses (null parameter list), it can also be called without the ampersand.

Example 11.2.

#!/usr/bin/perl
1   $name="Ellie";
2   print "Hello $name.\n";

3   bye();   # Without parens or an ampersand, bye would be a bareword
             # causing a warning message when -w is used.
4   sub bye{
5       print "Bye $name.\n";
    }

Forward Reference

A forward reference announces to the compiler that the subroutine has been defined somewhere in the program. The ampersand is not needed to call a subroutine if it has been forward referenced.

Example 11.3.

    #!/usr/bin/perl
1   sub bye;  # Forward reference

    $name="Ellie";
2   print "Hello $name.\n";

3   bye;      # Call subroutine without the ampersand

4   sub bye{
5       print "Bye $name\n";
    }
(Output)
2   Hello Ellie.
5   Bye Ellie

Scope of Variables

Scope describes where a variable is visible in your program. Perl variables are global in scope. They are visible throughout the entire program, even in subroutines. If you declare a variable in a subroutine, it is visible to the entire program. If you change the value of an already existing variable from within a subroutine, it will be changed when you exit the subroutine. A local variable is private to the block, subroutine, or file where it is declared. You must use either the local or my built-in functions to create local variables, since, by default, Perl variables are global in scope. (See "Call-by-Value with local and my" on page 332.)

Example 11.4.

(The Script)
    # Script: perlsub_sub2
    # Variables used in subroutines are global by default
1   sub bye { print "Bye $name\n"; $name="Tom";}
                               # Subroutine definition
2   $name="Ellie";
3   print "Hello to you and yours!\n";
4   &bye;
5   print "Out of the subroutine. Hello $name.\n";
                               # $name is now Tom
6   &bye;

(Output)
3   Hello to you and yours!
1   Bye Ellie
5   Out of the subroutine. Hello Tom.
1   Bye Tom

Explanation

  1. The subroutine bye is defined. Within the subroutine block, the variable $name is assigned the value Tom. $name is a global variable; in other words, it is visible throughout the program.[a]

    [a] We are assuming that the program was compiled into one package, main. For more on packages and scope, see Chapter 12, "Modularize It, Package It, and Send It to the Library!"

  2. Program execution starts here. Global variable $name is assigned the value Ellie.

  3. This line is here just to show you the flow of execution.

  4. The subroutine &bye is called. The program jumps into the subroutine on line 1. The value of $name is still Ellie. After the line Bye, Ellie is printed, and the variable $name is assigned a new value, Tom. The subroutine exits and the program resumes execution at line 5.

  5. The value of the global variable $name was changed in the subroutine.

  6. The subroutine is called again. The value of $name is Tom.

[a] We are assuming that the program was compiled into one package, main. For more on packages and scope, see Chapter 12, "Modularize It, Package It, and Send It to the Library!"

[a] We are assuming that the program was compiled into one package, main. For more on packages and scope, see Chapter 12, "Modularize It, Package It, and Send It to the Library!"

Previous Page Next Page