Personal Miscellaneous TCP/IP GRID Quality of Service  
VAIOHTMLLDAPC/C++LaTEX  

Subroutines

In the last program, the first six lines basically does the same thing twice? Computer programs are designed to perform repetitive tasks for you; if you find yourself typing the same thing over and over again in a program, it's a good clue that you should condense your program with a subroutine.

A subroutine is like a command you define yourself. So let's make a version of the program that uses a subroutine called getnumber that gets a number from the keyboard:

    
    #!/usr/bin/perl
    $x = &getnumber;
    $y = &getnumber;
    $z = $x + $y;
    print "$x + $y = $z\n";
    # The program ends there... # the subroutine is just tacked on at the end:
    sub getnumber { print "Type in a number: "; $number = <>; chop($number); $number; }

Everything inside the curly brackets is the subroutine. Now that section of the program will be run every time you use the command

&getnumber;

in your program. In this case, the subroutine "getnumber" returns a variable which is then assigned to $x and $y. Not all subroutines are designed to return variables. This one returns the contents of $number because of the line

$number;

So the command

$x = &getnumber;

first runs the subroutine, and then, when it sees the line $number; it exits the subroutine, and gives $x the value of $number.

But like I said, not all subroutines are designed to return a value. For instance, you could have a subroutine that just printed out a standard warning:

sub warning {
print "WARNING: The program is about to ask you to type in a number!\n";
}

So the line

&warning;

will now print out

WARNING: The program is about to ask you to type in a number!

every time you use it.

You can stick the code for subroutines anywhere you want in your program, but the traditional place is to group them all together at the end of the program, after the main section.


While

Right now the program just executes once and then kicks you out. Many times you'll want your program to do something over and over again; you can do this with a control structure that creates a "loop". There are many types of control structures; we'll start you off with a simple one called while. It basically means: do this set of commands while this statement is true. For instance, here's a code fragment that loops over and over as long as you type in the number "15":

$x = 15;
while($x == 15) {
$x = &getnumber;
}

Two things to note: 1) $x == 15 means "$x is equal to 15". It's a comparative statement; it should never be confused with $x = 15, which assigns the value 15 to $x. Confusing == and = is one of the most common novice errors; in this case, it would create an infinite loop.

2) The first line of the code fragment sets the value of $x to 15. If you didn't do that, $x wouldn't equal 15 the first time around, so the program would never enter the loop at all, and would never ask for a number.

Here's our adding program, with a loop so you can use it over and over:

    
    #!/usr/bin/perl
    $doagain = "yes"; while($doagain eq "yes") { $x = &getnumber; $y = &getnumber; $z = $x + $y;
    print "$x + $y = $z\n"> print "Do it again? (yes or no) ";
    $doagain = <>;
    chop($doagain); }

(I left out the subroutine to save space here, but you'd have to include it if you wanted that program to run.) Note that the comparative statement this time uses "eq" rather than "==". == compares for numerical values; eq is for variables made up of letters, numbers, and other characters (called "string" variables.)

 

Wed, 15 August, 2001 19:03 Previous PageNext Page
 
 
    email me!
© 2001-2003, Yee-Ting Li, email: ytl@hep.ucl.ac.uk, Tel: +44 (0) 20 7679 1376, Fax: +44 (0) 20 7679 7145
Room D14, High Energy Particle Physics, Dept. of Physics & Astronomy, UCL, Gower St, London, WC1E 6BT