Arguments Scoping and Subroutines

IT-SC 105 A subroutine is like a separate helper program for the main program, and it needs to have its own variables. You will use two types of variables in your subroutines in this book: [4] [4] In the subroutines in this book, we wont use global variables, which can be seen by both the main program and the subroutines; nor will we use variables declared with local , which provides a different kind of scoping restriction than my . Arguments passed in to the subroutine Other variables declared with my and restricted to the scope of the subroutine Arguments are the values given to a subroutine when it is used, or called. The values of the arguments are passed into the subroutine by means of the special variable _ , as youll see in the next section. Other variables a subroutine might use must be protected from interacting with variables in other parts of the program, so they have effect only within the subroutines own scope. This is accomplished by declaring them as my variables, as will be explained shortly. Finally, most subroutines return their results via the return function. This can return a single scalar as in return dna; in our subroutine addACGT, in a list of scalars as in return dna1, dna2; , in an array as in return lines; , and more.

6.2.1 Arguments

To call a subroutine means to type its name and give it appropriate arguments and, usually, collect its results. Arguments , sometimes called parameters, usually contain the data that the subroutine computes on. In Example 6-1 , this is the call of the subroutine addACGT with the argument dna : longer_dna = addACGTdna; The essential point is that whenever you, the programmer, want to use a subroutine, you can call it with whatever arguments it is designed to accept and with which you need to compute in this case, whatever DNA that needs ACGT appended to it and the value of each argument appears in the subroutine in the _ array. When you call a subroutine with certain arguments, the names of the arguments you provide in the call are not important inside the subroutine. Only the values of those arguments that are actually passed inside the subroutine are important. The subroutine typically collects the values from the _ array and assigns them to new variables that may or may not have the same names as the variables with which you called the subroutine. The only thing preserved is the order of the values, not the names of the variables containing the values. Heres how it works. The first line in the subroutines block is: IT-SC 106 mydna = _; The values of the arguments from the call of the subroutine are passed into the subroutine in the special array variable _ . You know its an array because it starts with the character. It has the brief name _, and its a special array variable that comes predefined in Perl programs. Its not a name you should pick for your own arrays. The array _ contains all the scalar values passed into the subroutine. These scalar values are the values of the arguments to the subroutine. In this case, there is one scalar value: the string of DNA thats the value of the variable dna passed in as an argument. If the subroutine has more arguments—for instance one argument for DNA, one for the associated protein, and one for the name of the gene—they are all passed in and assigned to my variables inside the subroutine: mydna,protein,name_of_gene = _; If there are no arguments, just omit that statement in the subroutine. After the statement: mydna = _; executes in the subroutine, the passed-in value is assigned to the subroutines variable dna . The next section explains why this is a new variable specific to the subroutine. The subroutines variable can be called anything; it certainly doesnt have to be the same name as the argument, as it happens to be in this example. Whats cool about scoping is that it doesnt matter if it is or not. Beware the common mistake of forgetting the _ array when naming your arguments in a subroutine, that is, using the statement: mydna; instead of: mydna = _; If you make this mistake, the values of the arguments wont appear in your subroutine, even though their names are declared.

6.2.2 Scoping